fix: 刷新 /admin 被错误跳转到 /profile
路由守卫在 user 信息未加载时判断权限,刷新后 token 从 localStorage 恢复但 user 为 null,导致 adminOnly 守卫误判 role !== 'admin' 而跳转。 改为在已登录但 user 为 null 时先 await fetchUser() 加载用户信息再判断。
This commit is contained in:
@@ -47,9 +47,14 @@ const router = createRouter({
|
||||
],
|
||||
})
|
||||
|
||||
router.beforeEach((to, from, next) => {
|
||||
router.beforeEach(async (to, from, next) => {
|
||||
const authStore = useAuthStore()
|
||||
|
||||
// 已登录但用户信息未加载(如刷新页面后),先获取用户信息
|
||||
if (authStore.isLoggedIn && !authStore.user) {
|
||||
await authStore.fetchUser()
|
||||
}
|
||||
|
||||
if (to.meta.requiresAuth && !authStore.isLoggedIn) {
|
||||
next({ name: 'login' })
|
||||
} else if (to.meta.adminOnly && authStore.user?.role !== 'admin') {
|
||||
|
||||
Reference in New Issue
Block a user