From cf1e3667eca5838ec49fda83eea9d6dcf892b23e Mon Sep 17 00:00:00 2001 From: sion123 <450702724@qq.com> Date: Sat, 28 Mar 2026 17:08:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- monisuo-admin/src/router/guard/auth-guard.ts | 21 +++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/monisuo-admin/src/router/guard/auth-guard.ts b/monisuo-admin/src/router/guard/auth-guard.ts index 5d82533..dfb8f8e 100644 --- a/monisuo-admin/src/router/guard/auth-guard.ts +++ b/monisuo-admin/src/router/guard/auth-guard.ts @@ -5,26 +5,33 @@ import { storeToRefs } from 'pinia' import pinia from '@/plugins/pinia/setup' import { useAuthStore } from '@/stores/auth' -// 需要认证的路由前缀 -const AUTH_ROUTES = ['/monisuo/', '/dashboard', '/users', '/tasks', '/settings'] +// 不需要认证的白名单路由 +const WHITE_LIST = ['/auth/sign-in', '/auth/sign-up', '/auth/forgot-password'] export function authGuard(router: Router) { router.beforeEach((to, _from) => { const authStore = useAuthStore(pinia) const { isLogin } = storeToRefs(authStore) - // 检查是否是需要认证的路由 - const needsAuth = to.meta.auth || AUTH_ROUTES.some(prefix => to.path.startsWith(prefix)) + // 检查是否在白名单中 + const isInWhiteList = WHITE_LIST.some(path => to.path === path || to.path.startsWith(path + '/')) - console.log('[AuthGuard]', to.path, 'needsAuth:', needsAuth, 'isLogin:', unref(isLogin)) + console.log('[AuthGuard]', to.path, 'isInWhiteList:', isInWhiteList, 'isLogin:', unref(isLogin)) - // 如果页面需要登录但用户未登录,重定向到登录页并记录原始目标页面 - if (needsAuth && !unref(isLogin) && to.name !== '/auth/sign-in') { + // 如果已登录且访问登录页,重定向到首页 + if (unref(isLogin) && to.path === '/auth/sign-in') { + return { name: '/monisuo/dashboard' } + } + + // 如果未登录且不在白名单中,重定向到登录页 + if (!unref(isLogin) && !isInWhiteList) { console.log('[AuthGuard] Redirecting to login') return { name: '/auth/sign-in', query: { redirect: to.fullPath }, } } + + return true }) }