修复会话安全与实时消息

This commit is contained in:
yml2213
2026-07-14 15:06:01 +08:00
parent 064af29d3b
commit 8cff2a5824
29 changed files with 1961 additions and 546 deletions
+9 -8
View File
@@ -3,15 +3,15 @@ import { setToken } from '@/services/request'
import { login as loginApi, type LoginResult } from '@/services/api'
interface AuthState {
user: LoginResult | null
loading: boolean
login: (username: string, password: string) => Promise<void>
user: LoginResult | null
loading: boolean
login: (username: string, password: string) => Promise<LoginResult>
logout: () => void
}
const AuthContext = createContext<AuthState>({
user: null, loading: false,
login: async () => {}, logout: () => {},
user: null, loading: false,
login: async () => { throw new Error('认证上下文未初始化') }, logout: () => {},
})
export function AuthProvider({ children }: { children: ReactNode }) {
@@ -31,9 +31,10 @@ export function AuthProvider({ children }: { children: ReactNode }) {
try {
const res = await loginApi({ username, password })
const u = res.data
setToken(u.token)
setUser(u)
localStorage.setItem('auth_user', JSON.stringify(u))
setToken(u.token)
setUser(u)
localStorage.setItem('auth_user', JSON.stringify(u))
return u
} finally {
setLoading(false)
}