feat(history): add multi-account and WhatsApp foundation
This commit is contained in:
+21
-1
@@ -2,7 +2,7 @@ import { create } from 'zustand'
|
||||
import { persist } from 'zustand/middleware'
|
||||
import type { AuthSession, Tenant, User } from '@/types'
|
||||
import type { RoleKey } from '@/config/roles'
|
||||
import { setTokenGetter } from '@/services/api/client'
|
||||
import { setTokenGetter, setUnauthorizedHandler } from '@/services/api/client'
|
||||
|
||||
interface AuthState {
|
||||
token: string | null
|
||||
@@ -32,6 +32,26 @@ export const useAuthStore = create<AuthState>()(
|
||||
|
||||
// 让 API client 能读取 token(真实后端模式使用)
|
||||
setTokenGetter(() => useAuthStore.getState().token)
|
||||
setUnauthorizedHandler(() => {
|
||||
useAuthStore.getState().logout()
|
||||
if (!window.location.pathname.startsWith('/login')) {
|
||||
window.location.replace('/login?reason=session-expired')
|
||||
}
|
||||
})
|
||||
|
||||
export function isSessionTokenExpired(token: string | null): boolean {
|
||||
if (!token) return true
|
||||
try {
|
||||
const encoded = token.split('.')[1].replace(/-/g, '+').replace(/_/g, '/')
|
||||
const padded = encoded.padEnd(Math.ceil(encoded.length / 4) * 4, '=')
|
||||
const payload = JSON.parse(window.atob(padded)) as {
|
||||
exp?: number
|
||||
}
|
||||
return !payload.exp || payload.exp * 1_000 <= Date.now()
|
||||
} catch {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
export function useCurrentRole(): RoleKey {
|
||||
return useAuthStore((s) => s.user?.role ?? 'viewer')
|
||||
|
||||
Reference in New Issue
Block a user