From 21f4ff65d9a70d2c6f1d183b79a64eefe7f2b251 Mon Sep 17 00:00:00 2001 From: liangweihao <734499798@qq.com> Date: Sat, 6 Sep 2025 10:02:47 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E5=90=8E=E7=AB=8B=E5=8D=B3=E9=AA=8C=E8=AF=81token=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改Dashboard组件,对于已登录用户跳过重复的token验证 - 在LoginForm中添加详细的认证状态日志 - 优化initAuth方法,避免对已登录用户的重复验证 - 添加认证状态检查,确保登录成功后再跳转 - 解决登录成功后立即调用/auth/me导致401的时序问题 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- frontend/src/components/LoginForm.vue | 18 ++++++++++++++++-- frontend/src/stores/auth.ts | 7 +++++++ frontend/src/views/Dashboard.vue | 21 +++++++++++++++++---- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/frontend/src/components/LoginForm.vue b/frontend/src/components/LoginForm.vue index 9f72f77..ec5114b 100644 --- a/frontend/src/components/LoginForm.vue +++ b/frontend/src/components/LoginForm.vue @@ -99,11 +99,18 @@ const handleLogin = async () => { } try { - await authStore.login({ + const response = await authStore.login({ username: loginForm.username, password: loginForm.password }) + console.log('登录成功,响应数据:', response) + console.log('Auth store状态:', { + isLoggedIn: authStore.isLoggedIn, + hasToken: !!authStore.token, + hasUser: !!authStore.user + }) + // 如果选择记住我,保存登录信息到本地存储 if (loginForm.rememberMe) { localStorage.setItem('rememberedUser', JSON.stringify({ @@ -114,7 +121,14 @@ const handleLogin = async () => { localStorage.removeItem('rememberedUser') } - router.push('/dashboard') + // 确保状态已正确设置后再跳转 + if (authStore.isLoggedIn) { + console.log('认证状态确认成功,即将跳转到Dashboard') + router.push('/dashboard') + } else { + console.error('登录后认证状态异常') + toast.error('登录状态异常,请重试') + } } catch (error: any) { console.error('登录失败,详细错误信息:', { status: error.response?.status, diff --git a/frontend/src/stores/auth.ts b/frontend/src/stores/auth.ts index 2ef5bf0..cfd0473 100644 --- a/frontend/src/stores/auth.ts +++ b/frontend/src/stores/auth.ts @@ -16,10 +16,17 @@ export const useAuthStore = defineStore('auth', () => { // 初始化认证状态 const initAuth = async () => { + // 如果已经登录,不需要重新初始化 + if (isLoggedIn.value) { + console.log('用户已登录,跳过初始化') + return + } + const storedToken = userAuth.getToken() const storedUser = userAuth.getUserInfo() if (storedToken && storedUser) { + console.log('从本地存储恢复登录状态') token.value = storedToken user.value = storedUser try { diff --git a/frontend/src/views/Dashboard.vue b/frontend/src/views/Dashboard.vue index 5ce83f2..613d20a 100644 --- a/frontend/src/views/Dashboard.vue +++ b/frontend/src/views/Dashboard.vue @@ -201,8 +201,22 @@ const loadUserAccounts = async () => { // 组件挂载时获取数据 onMounted(async () => { try { - // 初始化认证状态 - await authStore.initAuth() + console.log('Dashboard初始化开始,当前认证状态:', { + isLoggedIn: authStore.isLoggedIn, + hasToken: !!authStore.token, + hasUser: !!authStore.user, + hasLocalToken: !!localStorage.getItem('userToken'), + hasLocalUser: !!localStorage.getItem('userInfo') + }) + + // 如果用户已经登录(比如刚从登录页面跳转过来),直接使用当前状态 + if (authStore.isLoggedIn) { + console.log('用户已登录,直接加载账号数据') + } else { + // 否则尝试从localStorage恢复状态 + console.log('用户未登录,尝试初始化认证状态') + await authStore.initAuth() + } // 确保用户已登录 if (!authStore.isLoggedIn) { @@ -211,8 +225,7 @@ onMounted(async () => { return } - // initAuth() 已经验证过token,无需再次调用getProfile() - // 直接加载用户账号 + // 加载用户账号 await loadUserAccounts() } catch (error: any) { console.error('Dashboard初始化失败:', error)