diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts index 5ecf804..41cedd9 100644 --- a/frontend/src/router/index.ts +++ b/frontend/src/router/index.ts @@ -67,7 +67,7 @@ const router = createRouter({ // 路由守卫 router.beforeEach(async ( to: RouteLocationNormalized, - _from: RouteLocationNormalized, + from: RouteLocationNormalized, next: NavigationGuardNext ) => { // 设置页面标题 @@ -76,9 +76,12 @@ router.beforeEach(async ( const authStore = useAuthStore() + console.log(`路由守卫: ${from.path} -> ${to.path}, 用户登录状态: ${authStore.isLoggedIn}`) + // 检查是否需要用户认证 if (to.meta.requiresAuth) { if (!authStore.isLoggedIn) { + console.log('需要登录,重定向到首页') next('/') return } @@ -87,6 +90,7 @@ router.beforeEach(async ( // 检查是否需要管理员认证 if (to.meta.requiresAdminAuth) { if (!adminAuth.isLoggedIn()) { + console.log('需要管理员登录,重定向到管理员登录页') next('/admin/login') return } @@ -94,6 +98,7 @@ router.beforeEach(async ( // 用户已登录且访问首页,重定向到dashboard if (to.path === '/' && authStore.isLoggedIn) { + console.log('用户已登录,重定向到dashboard') next({ name: 'Dashboard' }) return } diff --git a/frontend/src/stores/auth.ts b/frontend/src/stores/auth.ts index 0f31e5c..2ef5bf0 100644 --- a/frontend/src/stores/auth.ts +++ b/frontend/src/stores/auth.ts @@ -23,11 +23,17 @@ export const useAuthStore = defineStore('auth', () => { token.value = storedToken user.value = storedUser try { + // 验证token有效性 await getProfile() - } catch (error) { + console.log('Token验证成功,用户已登录') + } catch (error: any) { + console.log('Token验证失败,清除本地存储:', error.response?.status) // token失效,清除本地存储 - logout() + await logout() + throw error // 重新抛出错误,让调用方知道初始化失败 } + } else { + console.log('本地存储中无有效的登录信息') } } diff --git a/frontend/src/utils/api.ts b/frontend/src/utils/api.ts index 91746f8..c97528e 100644 --- a/frontend/src/utils/api.ts +++ b/frontend/src/utils/api.ts @@ -41,17 +41,25 @@ api.interceptors.response.use( }, (error) => { if (error.response?.status === 401) { - // 如果是登出请求,不要自动重定向 - if (error.config?.url?.includes('/auth/logout')) { + // 如果是登出请求或获取用户信息的请求,不要自动重定向,让上层处理 + if (error.config?.url?.includes('/auth/logout') || + error.config?.url?.includes('/auth/me')) { return Promise.reject(error) } + console.log('收到401响应,URL:', error.config?.url) + // Token过期或无效,清除所有认证状态 userAuth.logout() adminAuth.logout() - // 自动重定向到登录页面(如果不是已经在登录页面) - if (window.location.pathname !== '/' && window.location.pathname !== '/login') { + // 只有在非登录相关页面时才自动重定向 + const currentPath = window.location.pathname + if (currentPath !== '/' && + currentPath !== '/login' && + currentPath !== '/admin/login' && + !currentPath.includes('/auth')) { + console.log('自动重定向到首页') window.location.href = '/' } } diff --git a/frontend/src/views/Dashboard.vue b/frontend/src/views/Dashboard.vue index 67f7882..5ce83f2 100644 --- a/frontend/src/views/Dashboard.vue +++ b/frontend/src/views/Dashboard.vue @@ -211,22 +211,8 @@ onMounted(async () => { return } - // 验证token是否有效(通过尝试获取用户信息) - try { - await authStore.getProfile() - } catch (error: any) { - if (error.response?.status === 401) { - // Token无效,清除认证状态并重定向 - authStore.logout() - toast.error('登录已过期,请重新登录') - router.push('/') - return - } - // 其他错误,继续尝试加载账号 - console.warn('获取用户信息失败,但继续加载账号:', error) - } - - // 加载用户账号 + // initAuth() 已经验证过token,无需再次调用getProfile() + // 直接加载用户账号 await loadUserAccounts() } catch (error: any) { console.error('Dashboard初始化失败:', error)