From 14e47bb35ea2bdfc9f3bb94ed8a572096ffdf018 Mon Sep 17 00:00:00 2001 From: bluish <734499798@qq.com> Date: Mon, 17 Nov 2025 15:06:52 +0000 Subject: [PATCH] revert f583f787f09940a21f3276ef7b36327e89ef9847 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit revert fix: 修复登录后跳转然后返回登录页的问题 - 移除Dashboard组件中重复的token验证调用 - 优化auth store的initAuth方法,增加详细错误处理和日志 - 改进API响应拦截器,减少对/auth/me请求的自动重定向 - 添加路由守卫调试日志,方便定位路由跳转问题 - 解决登录时的竞态条件和重复验证问题 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- frontend/src/router/index.ts | 7 +------ frontend/src/stores/auth.ts | 10 ++-------- frontend/src/utils/api.ts | 16 ++++------------ frontend/src/views/Dashboard.vue | 18 ++++++++++++++++-- 4 files changed, 23 insertions(+), 28 deletions(-) diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts index 41cedd9..5ecf804 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,12 +76,9 @@ 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 } @@ -90,7 +87,6 @@ router.beforeEach(async ( // 检查是否需要管理员认证 if (to.meta.requiresAdminAuth) { if (!adminAuth.isLoggedIn()) { - console.log('需要管理员登录,重定向到管理员登录页') next('/admin/login') return } @@ -98,7 +94,6 @@ 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 2ef5bf0..0f31e5c 100644 --- a/frontend/src/stores/auth.ts +++ b/frontend/src/stores/auth.ts @@ -23,17 +23,11 @@ export const useAuthStore = defineStore('auth', () => { token.value = storedToken user.value = storedUser try { - // 验证token有效性 await getProfile() - console.log('Token验证成功,用户已登录') - } catch (error: any) { - console.log('Token验证失败,清除本地存储:', error.response?.status) + } catch (error) { // token失效,清除本地存储 - await logout() - throw error // 重新抛出错误,让调用方知道初始化失败 + logout() } - } else { - console.log('本地存储中无有效的登录信息') } } diff --git a/frontend/src/utils/api.ts b/frontend/src/utils/api.ts index c97528e..91746f8 100644 --- a/frontend/src/utils/api.ts +++ b/frontend/src/utils/api.ts @@ -41,25 +41,17 @@ api.interceptors.response.use( }, (error) => { if (error.response?.status === 401) { - // 如果是登出请求或获取用户信息的请求,不要自动重定向,让上层处理 - if (error.config?.url?.includes('/auth/logout') || - error.config?.url?.includes('/auth/me')) { + // 如果是登出请求,不要自动重定向 + if (error.config?.url?.includes('/auth/logout')) { return Promise.reject(error) } - console.log('收到401响应,URL:', error.config?.url) - // Token过期或无效,清除所有认证状态 userAuth.logout() adminAuth.logout() - // 只有在非登录相关页面时才自动重定向 - const currentPath = window.location.pathname - if (currentPath !== '/' && - currentPath !== '/login' && - currentPath !== '/admin/login' && - !currentPath.includes('/auth')) { - console.log('自动重定向到首页') + // 自动重定向到登录页面(如果不是已经在登录页面) + if (window.location.pathname !== '/' && window.location.pathname !== '/login') { window.location.href = '/' } } diff --git a/frontend/src/views/Dashboard.vue b/frontend/src/views/Dashboard.vue index 5ce83f2..67f7882 100644 --- a/frontend/src/views/Dashboard.vue +++ b/frontend/src/views/Dashboard.vue @@ -211,8 +211,22 @@ onMounted(async () => { return } - // initAuth() 已经验证过token,无需再次调用getProfile() - // 直接加载用户账号 + // 验证token是否有效(通过尝试获取用户信息) + try { + await authStore.getProfile() + } catch (error: any) { + if (error.response?.status === 401) { + // Token无效,清除认证状态并重定向 + authStore.logout() + toast.error('登录已过期,请重新登录') + router.push('/') + return + } + // 其他错误,继续尝试加载账号 + console.warn('获取用户信息失败,但继续加载账号:', error) + } + + // 加载用户账号 await loadUserAccounts() } catch (error: any) { console.error('Dashboard初始化失败:', error)