Skip to content

Commit de2ff9b

Browse files
committed
fix: register router guard
1 parent b747e98 commit de2ff9b

1 file changed

Lines changed: 18 additions & 15 deletions

File tree

apps/frontend/src/router/index.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,28 +80,31 @@ router.beforeEach((to, _, next) => {
8080
const globalStore = useGlobalStore()
8181

8282
const isPublicRoute = PUBLIC_ROUTES.includes(to.name as string)
83-
const goToDashboard = AUTH_REDIRECT_ROUTES.includes(to.name as string)
83+
const shouldRedirectToDashboard = AUTH_REDIRECT_ROUTES.includes(to.name as string)
8484

85-
if (userStore.isAuthenticated && goToDashboard) {
86-
next({ name: 'Dashboard' })
87-
}
88-
else if (isPublicRoute) {
89-
next()
90-
}
91-
else if (userStore.isAuthenticated) {
92-
next()
93-
}
94-
else {
95-
next({ name: 'Login' })
85+
// 1. 已认证用户访问登录/注册等页面 → 重定向到 Dashboard
86+
if (userStore.isAuthenticated && shouldRedirectToDashboard) {
87+
return next({ name: 'Dashboard' })
9688
}
9789

90+
// 2. 注册页面但注册功能已关闭 → 重定向到登录
9891
if (to.name === 'Register' && !globalStore.systemConfig.register_enabled) {
99-
next({ name: 'Login' })
10092
toast.error('注册功能已关闭', { description: '请联系管理员注册新账号' })
93+
return next({ name: 'Login' })
10194
}
102-
else {
103-
next()
95+
96+
// 3. 公开路由 → 直接放行
97+
if (isPublicRoute) {
98+
return next()
10499
}
100+
101+
// 4. 已认证用户 → 放行
102+
if (userStore.isAuthenticated) {
103+
return next()
104+
}
105+
106+
// 5. 未认证用户访问受保护路由 → 重定向到登录
107+
return next({ name: 'Login' })
105108
})
106109

107110
export default router

0 commit comments

Comments
 (0)