From 840e5f0898082430b7c572457c84eeffa2265298 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=80=E5=AF=B8=E7=81=B0?= Date: Mon, 9 Dec 2024 18:42:37 +0800 Subject: [PATCH] refactor(projects): :recycle: sync refactor router guard. (#17) --- .env | 2 + src/router/guard/route.ts | 169 ++++++++++++++++---------------------- 2 files changed, 75 insertions(+), 96 deletions(-) diff --git a/.env b/.env index ace03e3..d2d7f54 100644 --- a/.env +++ b/.env @@ -1,3 +1,5 @@ +# the base url of the application, the default is "/" +# if use a sub directory, it must be end with "/", like "/admin/" but not "/admin" VITE_BASE_URL=/ VITE_APP_TITLE=SoybeanAdmin diff --git a/src/router/guard/route.ts b/src/router/guard/route.ts index 68f3604..a6b7b9d 100644 --- a/src/router/guard/route.ts +++ b/src/router/guard/route.ts @@ -36,54 +36,34 @@ export function createRouteGuard(router: Router) { const routeRoles = to.meta.roles || []; const hasRole = authStore.userInfo.roles.some(role => routeRoles.includes(role)); - const hasAuth = authStore.isStaticSuper || !routeRoles.length || hasRole; - const routeSwitches: CommonType.StrategicPattern[] = [ - // if it is login route when logged in, then switch to the root page - { - condition: isLogin && to.name === loginRoute, - callback: () => { - next({ name: rootRoute }); - } - }, - // if it is constant route, then it is allowed to access directly - { - condition: !needLogin, - callback: () => { - handleRouteSwitch(to, from, next); - } - }, - // if the route need login but the user is not logged in, then switch to the login page - { - condition: !isLogin && needLogin, - callback: () => { - next({ name: loginRoute, query: { redirect: to.fullPath } }); - } - }, - // if the user is logged in and has authorization, then it is allowed to access - { - condition: isLogin && needLogin && hasAuth, - callback: () => { - handleRouteSwitch(to, from, next); - } - }, - // if the user is logged in but does not have authorization, then switch to the 403 page - { - condition: isLogin && needLogin && !hasAuth, - callback: () => { - next({ name: noAuthorizationRoute }); - } - } - ]; - - routeSwitches.some(({ condition, callback }) => { - if (condition) { - callback(); - } - - return condition; - }); + // if it is login route when logged in, then switch to the root page + if (to.name === loginRoute && isLogin) { + next({ name: rootRoute }); + return; + } + + // if the route does not need login, then it is allowed to access directly + if (!needLogin) { + handleRouteSwitch(to, from, next); + return; + } + + // the route need login but the user is not logged in, then switch to the login page + if (!isLogin) { + next({ name: loginRoute, query: { redirect: to.fullPath } }); + return; + } + + // if the user is logged in but does not have authorization, then switch to the 403 page + if (!hasAuth) { + next({ name: noAuthorizationRoute }); + return; + } + + // switch route normally + handleRouteSwitch(to, from, next); }); } @@ -93,7 +73,6 @@ export function createRouteGuard(router: Router) { * @param to to route */ async function initRoute(to: RouteLocationNormalized): Promise { - const authStore = useAuthStore(); const routeStore = useRouteStore(); const notFoundRoute: RouteKey = 'not-found'; @@ -105,50 +84,28 @@ async function initRoute(to: RouteLocationNormalized): Promise