From 170df5f457da06415f9df22eef0546618d809077 Mon Sep 17 00:00:00 2001 From: kevin Date: Wed, 8 Jul 2026 12:00:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=88=B7=E6=96=B0=20/admin=20=E8=A2=AB?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E8=B7=B3=E8=BD=AC=E5=88=B0=20/profile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 路由守卫在 user 信息未加载时判断权限,刷新后 token 从 localStorage 恢复但 user 为 null,导致 adminOnly 守卫误判 role !== 'admin' 而跳转。 改为在已登录但 user 为 null 时先 await fetchUser() 加载用户信息再判断。 --- frontend/src/router/index.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts index 8c0e2d0..b399792 100644 --- a/frontend/src/router/index.ts +++ b/frontend/src/router/index.ts @@ -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') {