From ddaf4b786391867bc400dbfcd29acbd3bde3c87b Mon Sep 17 00:00:00 2001 From: haitaoo Date: Tue, 25 Apr 2023 10:52:37 +0800 Subject: [PATCH] fix(guard): Unifying the definition and use of `IGNORE_PATH_LIST` --- ui/src/common/constants.ts | 12 ------------ ui/src/router/alias.ts | 8 +++++++- ui/src/utils/guard.ts | 31 +++++++++++++++---------------- ui/src/utils/request.ts | 6 +++--- 4 files changed, 25 insertions(+), 32 deletions(-) diff --git a/ui/src/common/constants.ts b/ui/src/common/constants.ts index 727b91b9..35643bda 100644 --- a/ui/src/common/constants.ts +++ b/ui/src/common/constants.ts @@ -15,18 +15,6 @@ export const USER_AGENT_NAMES = { DingTalk: 'DingTalk', }; -export const IGNORE_PATH_LIST = [ - '/users/login', - '/users/register', - '/users/account-recovery', - '/users/change-email', - '/users/password-reset', - '/users/account-activation', - '/users/account-activation/success', - '/users/account-activation/failed', - '/users/confirm-new-email', -]; - export const ADMIN_LIST_STATUS = { // normal; 1: { diff --git a/ui/src/router/alias.ts b/ui/src/router/alias.ts index a21ec87b..1c0a3eeb 100644 --- a/ui/src/router/alias.ts +++ b/ui/src/router/alias.ts @@ -2,7 +2,13 @@ export const RouteAlias = { home: '/', login: '/users/login', signUp: '/users/register', - activation: '/users/login?status=inactive', + inactive: '/users/login?status=inactive', + accountRecovery: '/users/account-recovery', + changeEmail: '/users/change-email', + passwordReset: '/users/password-reset', + accountActivation: '/users/account-activation', + activationSuccess: '/users/account-activation/success', activationFailed: '/users/account-activation/failed', suspended: '/users/account-suspended', + confirmNewEmail: '/users/confirm-new-email', }; diff --git a/ui/src/utils/guard.ts b/ui/src/utils/guard.ts index 7f016f91..7c29aff6 100644 --- a/ui/src/utils/guard.ts +++ b/ui/src/utils/guard.ts @@ -81,6 +81,17 @@ export const deriveLoginState = (): TLoginState => { return ls; }; +export const IGNORE_PATH_LIST = [ + RouteAlias.login, + RouteAlias.signUp, + RouteAlias.accountRecovery, + RouteAlias.changeEmail, + RouteAlias.passwordReset, + RouteAlias.accountActivation, + RouteAlias.confirmNewEmail, + '/user-center/', +]; + export const isIgnoredPath = (ignoredPath: string | string[]) => { if (!Array.isArray(ignoredPath)) { ignoredPath = [ignoredPath]; @@ -152,7 +163,7 @@ export const activated = () => { const us = deriveLoginState(); if (us.isNotActivated) { gr.ok = false; - gr.redirect = RouteAlias.activation; + gr.redirect = RouteAlias.inactive; } return gr; }; @@ -258,19 +269,7 @@ export const shouldLoginRequired = () => { if (us.isLogged) { return gr; } - if ( - isIgnoredPath([ - RouteAlias.login, - RouteAlias.signUp, - '/users/account-recovery', - 'users/change-email', - 'users/password-reset', - 'users/account-activation', - 'users/account-activation/success', - '/users/account-activation/failed', - '/users/confirm-new-email', - ]) - ) { + if (isIgnoredPath(IGNORE_PATH_LIST)) { return gr; } gr.ok = false; @@ -296,7 +295,7 @@ export const tryNormalLogged = (canNavigate: boolean = false) => { return false; } if (us.isNotActivated) { - floppyNavigation.navigate(RouteAlias.activation); + floppyNavigation.navigate(RouteAlias.inactive); } else if (us.isForbidden) { floppyNavigation.navigate(RouteAlias.suspended, { handler: 'replace', @@ -341,7 +340,7 @@ export const handleLoginWithToken = ( loggedUserInfoStore.getState().update(res); const userStat = deriveLoginState(); if (userStat.isNotActivated) { - floppyNavigation.navigate(RouteAlias.activation, { + floppyNavigation.navigate(RouteAlias.inactive, { handler, options: { replace: true, diff --git a/ui/src/utils/request.ts b/ui/src/utils/request.ts index 739d3b1b..21fb7254 100644 --- a/ui/src/utils/request.ts +++ b/ui/src/utils/request.ts @@ -3,13 +3,13 @@ import type { AxiosInstance, AxiosRequestConfig, AxiosError } from 'axios'; import { Modal } from '@/components'; import { loggedUserInfoStore, toastStore, errorCodeStore } from '@/stores'; -import { LOGGED_TOKEN_STORAGE_KEY, IGNORE_PATH_LIST } from '@/common/constants'; +import { LOGGED_TOKEN_STORAGE_KEY } from '@/common/constants'; import { RouteAlias } from '@/router/alias'; import { getCurrentLang } from '@/utils/localize'; import Storage from './storage'; import { floppyNavigation } from './floppyNavigation'; -import { isIgnoredPath } from './guard'; +import { isIgnoredPath, IGNORE_PATH_LIST } from './guard'; const baseConfig = { timeout: 10000, @@ -129,7 +129,7 @@ class Request { } if (data?.type === 'inactive') { // inactivated - floppyNavigation.navigate(RouteAlias.activation); + floppyNavigation.navigate(RouteAlias.inactive); return Promise.reject(false); }