From 268ee4d055a790483192f2dafd264a4fbe9ff185 Mon Sep 17 00:00:00 2001 From: bluish <734499798@qq.com> Date: Mon, 17 Nov 2025 15:03:18 +0000 Subject: [PATCH] revert 21f4ff65d9a70d2c6f1d183b79a64eefe7f2b251 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit revert fix: 修复登录后立即验证token失败的问题 - 修改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, 6 insertions(+), 40 deletions(-) diff --git a/frontend/src/components/LoginForm.vue b/frontend/src/components/LoginForm.vue index ec5114b..9f72f77 100644 --- a/frontend/src/components/LoginForm.vue +++ b/frontend/src/components/LoginForm.vue @@ -99,18 +99,11 @@ const handleLogin = async () => { } try { - const response = await authStore.login({ + 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({ @@ -121,14 +114,7 @@ const handleLogin = async () => { localStorage.removeItem('rememberedUser') } - // 确保状态已正确设置后再跳转 - if (authStore.isLoggedIn) { - console.log('认证状态确认成功,即将跳转到Dashboard') - router.push('/dashboard') - } else { - console.error('登录后认证状态异常') - toast.error('登录状态异常,请重试') - } + router.push('/dashboard') } catch (error: any) { console.error('登录失败,详细错误信息:', { status: error.response?.status, diff --git a/frontend/src/stores/auth.ts b/frontend/src/stores/auth.ts index cfd0473..2ef5bf0 100644 --- a/frontend/src/stores/auth.ts +++ b/frontend/src/stores/auth.ts @@ -16,17 +16,10 @@ 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 613d20a..5ce83f2 100644 --- a/frontend/src/views/Dashboard.vue +++ b/frontend/src/views/Dashboard.vue @@ -201,22 +201,8 @@ const loadUserAccounts = async () => { // 组件挂载时获取数据 onMounted(async () => { try { - 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() - } + // 初始化认证状态 + await authStore.initAuth() // 确保用户已登录 if (!authStore.isLoggedIn) { @@ -225,7 +211,8 @@ onMounted(async () => { return } - // 加载用户账号 + // initAuth() 已经验证过token,无需再次调用getProfile() + // 直接加载用户账号 await loadUserAccounts() } catch (error: any) { console.error('Dashboard初始化失败:', error)