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:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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('本地存储中无有效的登录信息')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 = '/'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user