fix(guards): 修复 SPA 直接导航重定向到登录页

authLoader 中 initFromStorage() 后需重新调用 getState() 获取最新状态,
否则 isAuthenticated 检查使用过时的快照值导致已登录用户被重定向到登录页。
同步修复 usePermissionStore 的相同问题。
This commit is contained in:
张翔
2026-05-06 14:15:02 +08:00
committed by zhangxiang
parent e2b332dda7
commit 000a137a3d
+5 -2
View File
@@ -9,17 +9,18 @@ export async function authLoader() {
return redirect('/login')
}
const authState = useAuthStore.getState()
let authState = useAuthStore.getState()
if (!authState.initialized) {
authState.initFromStorage()
authState = useAuthStore.getState()
}
if (!authState.isAuthenticated) {
return redirect('/login')
}
const permState = usePermissionStore.getState()
let permState = usePermissionStore.getState()
if (!permState.loaded) {
const restored = permState.initFromStorage()
@@ -27,10 +28,12 @@ export async function authLoader() {
try {
await permState.fetchUserMenus()
} catch {
authState = useAuthStore.getState()
authState.logout()
return redirect('/login')
}
}
permState = usePermissionStore.getState()
}
return null