Files
JKVideo/store/authStore.ts
Developer 6e0dc2729c 本项目已收到哔哩哔哩(bilibili)律师函,要求停止对 B 站 API 的调用及相关仿制行为。
为尊重知识产权及相关法律法规,本仓库即日起停止后续维护与更新,不再接受新的 Issue 和 Pull Request。

现有代码仅作学习参考保留,请勿将本项目用于任何商业或违法用途。

感谢所有支持过本项目的朋友。
2026-05-12 21:29:45 +08:00

81 lines
2.7 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { create } from 'zustand';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { getUserInfo } from '../services/bilibili';
import { getSecure, setSecure, deleteSecure } from '../utils/secureStorage';
interface AuthState {
sessdata: string | null;
/** B 站 CSRF tokenbili_jct cookie 值)。写操作(关注/点赞/投币 etc必填 */
biliJct: string | null;
uid: string | null;
username: string | null;
face: string | null;
isLoggedIn: boolean;
login: (sessdata: string, uid: string, username?: string, biliJct?: string) => Promise<void>;
logout: () => Promise<void>;
restore: () => Promise<void>;
setProfile: (face: string, username: string, uid: string) => void;
}
export const useAuthStore = create<AuthState>((set) => ({
sessdata: null,
biliJct: null,
uid: null,
username: null,
face: null,
isLoggedIn: false,
login: async (sessdata, uid, username, biliJct) => {
await setSecure('SESSDATA', sessdata);
if (biliJct) await setSecure('bili_jct', biliJct);
// Migrate: remove SESSDATA from AsyncStorage if it was there before
await AsyncStorage.removeItem('SESSDATA').catch(() => { });
set({
sessdata,
biliJct: biliJct ?? null,
uid: uid || null,
username: username || null,
isLoggedIn: true,
});
getUserInfo().then(async (info) => {
await AsyncStorage.multiSet([
['UID', String(info.mid)],
['USERNAME', info.uname],
['FACE', info.face],
]).catch(() => { });
set({ face: info.face, username: info.uname, uid: String(info.mid) });
}).catch(() => { });
},
logout: async () => {
await deleteSecure('SESSDATA');
await deleteSecure('bili_jct');
await AsyncStorage.multiRemove(['UID', 'USERNAME', 'FACE']);
set({ sessdata: null, biliJct: null, uid: null, username: null, face: null, isLoggedIn: false });
},
restore: async () => {
// Try SecureStore first, fallback to AsyncStorage for migration
let sessdata = await getSecure('SESSDATA');
if (!sessdata) {
sessdata = await AsyncStorage.getItem('SESSDATA');
if (sessdata) {
// Migrate from AsyncStorage to SecureStore
await setSecure('SESSDATA', sessdata);
await AsyncStorage.removeItem('SESSDATA');
}
}
const biliJct = await getSecure('bili_jct');
if (sessdata) {
set({ sessdata, biliJct, isLoggedIn: true });
try {
const info = await getUserInfo();
await AsyncStorage.setItem('FACE', info.face);
set({ face: info.face, username: info.uname, uid: String(info.mid) });
} catch { }
}
},
setProfile: (face, username, uid) => set({ face, username, uid }),
}));