fix(guards): 修复 SPA 直接导航重定向到登录页
authLoader 中 initFromStorage() 后需重新调用 getState() 获取最新状态, 否则 isAuthenticated 检查使用过时的快照值导致已登录用户被重定向到登录页。 同步修复 usePermissionStore 的相同问题。
This commit is contained in:
@@ -9,17 +9,18 @@ export async function authLoader() {
|
|||||||
return redirect('/login')
|
return redirect('/login')
|
||||||
}
|
}
|
||||||
|
|
||||||
const authState = useAuthStore.getState()
|
let authState = useAuthStore.getState()
|
||||||
|
|
||||||
if (!authState.initialized) {
|
if (!authState.initialized) {
|
||||||
authState.initFromStorage()
|
authState.initFromStorage()
|
||||||
|
authState = useAuthStore.getState()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!authState.isAuthenticated) {
|
if (!authState.isAuthenticated) {
|
||||||
return redirect('/login')
|
return redirect('/login')
|
||||||
}
|
}
|
||||||
|
|
||||||
const permState = usePermissionStore.getState()
|
let permState = usePermissionStore.getState()
|
||||||
|
|
||||||
if (!permState.loaded) {
|
if (!permState.loaded) {
|
||||||
const restored = permState.initFromStorage()
|
const restored = permState.initFromStorage()
|
||||||
@@ -27,10 +28,12 @@ export async function authLoader() {
|
|||||||
try {
|
try {
|
||||||
await permState.fetchUserMenus()
|
await permState.fetchUserMenus()
|
||||||
} catch {
|
} catch {
|
||||||
|
authState = useAuthStore.getState()
|
||||||
authState.logout()
|
authState.logout()
|
||||||
return redirect('/login')
|
return redirect('/login')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
permState = usePermissionStore.getState()
|
||||||
}
|
}
|
||||||
|
|
||||||
return null
|
return null
|
||||||
|
|||||||
Reference in New Issue
Block a user