fix: 修复登录后跳转然后返回登录页的问题

- 移除Dashboard组件中重复的token验证调用
- 优化auth store的initAuth方法,增加详细错误处理和日志
- 改进API响应拦截器,减少对/auth/me请求的自动重定向
- 添加路由守卫调试日志,方便定位路由跳转问题
- 解决登录时的竞态条件和重复验证问题

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-09-06 09:43:57 +08:00
parent 0edceecfe5
commit f583f787f0
4 changed files with 28 additions and 23 deletions

View File

@@ -67,7 +67,7 @@ const router = createRouter({
// 路由守卫 // 路由守卫
router.beforeEach(async ( router.beforeEach(async (
to: RouteLocationNormalized, to: RouteLocationNormalized,
_from: RouteLocationNormalized, from: RouteLocationNormalized,
next: NavigationGuardNext next: NavigationGuardNext
) => { ) => {
// 设置页面标题 // 设置页面标题
@@ -76,9 +76,12 @@ router.beforeEach(async (
const authStore = useAuthStore() const authStore = useAuthStore()
console.log(`路由守卫: ${from.path} -> ${to.path}, 用户登录状态: ${authStore.isLoggedIn}`)
// 检查是否需要用户认证 // 检查是否需要用户认证
if (to.meta.requiresAuth) { if (to.meta.requiresAuth) {
if (!authStore.isLoggedIn) { if (!authStore.isLoggedIn) {
console.log('需要登录,重定向到首页')
next('/') next('/')
return return
} }
@@ -87,6 +90,7 @@ router.beforeEach(async (
// 检查是否需要管理员认证 // 检查是否需要管理员认证
if (to.meta.requiresAdminAuth) { if (to.meta.requiresAdminAuth) {
if (!adminAuth.isLoggedIn()) { if (!adminAuth.isLoggedIn()) {
console.log('需要管理员登录,重定向到管理员登录页')
next('/admin/login') next('/admin/login')
return return
} }
@@ -94,6 +98,7 @@ router.beforeEach(async (
// 用户已登录且访问首页重定向到dashboard // 用户已登录且访问首页重定向到dashboard
if (to.path === '/' && authStore.isLoggedIn) { if (to.path === '/' && authStore.isLoggedIn) {
console.log('用户已登录重定向到dashboard')
next({ name: 'Dashboard' }) next({ name: 'Dashboard' })
return return
} }

View File

@@ -23,11 +23,17 @@ export const useAuthStore = defineStore('auth', () => {
token.value = storedToken token.value = storedToken
user.value = storedUser user.value = storedUser
try { try {
// 验证token有效性
await getProfile() await getProfile()
} catch (error) { console.log('Token验证成功用户已登录')
} catch (error: any) {
console.log('Token验证失败清除本地存储:', error.response?.status)
// token失效清除本地存储 // token失效清除本地存储
logout() await logout()
throw error // 重新抛出错误,让调用方知道初始化失败
} }
} else {
console.log('本地存储中无有效的登录信息')
} }
} }

View File

@@ -41,17 +41,25 @@ api.interceptors.response.use(
}, },
(error) => { (error) => {
if (error.response?.status === 401) { 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) return Promise.reject(error)
} }
console.log('收到401响应URL:', error.config?.url)
// Token过期或无效清除所有认证状态 // Token过期或无效清除所有认证状态
userAuth.logout() userAuth.logout()
adminAuth.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 = '/' window.location.href = '/'
} }
} }

View File

@@ -211,22 +211,8 @@ onMounted(async () => {
return return
} }
// 验证token是否有效(通过尝试获取用户信息) // initAuth() 已经验证token无需再次调用getProfile()
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() await loadUserAccounts()
} catch (error: any) { } catch (error: any) {
console.error('Dashboard初始化失败:', error) console.error('Dashboard初始化失败:', error)