fix(guards): 修复 SPA 直接导航重定向到登录页
authLoader 中 initFromStorage() 后需重新调用 getState() 获取最新状态, 否则 isAuthenticated 检查使用过时的快照值导致已登录用户被重定向到登录页。 同步修复 usePermissionStore 的相同问题。
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user