From e4e508ac8e3735d7ab9071a390778216d3814dce Mon Sep 17 00:00:00 2001 From: Sheldon Guo Date: Mon, 10 Mar 2025 16:52:08 +0800 Subject: [PATCH] feat: move AuthErrorCode to constants and update imports --- packages/core/auth/src/auth.ts | 12 +---------- packages/core/auth/src/base/auth.ts | 3 ++- packages/core/auth/src/client.ts | 2 +- packages/core/auth/src/constants/index.ts | 20 +++++++++++++++++++ packages/core/auth/src/index.ts | 1 + .../src/nocobase-buildin-plugin/index.tsx | 3 ++- .../plugin-auth/src/client/interceptors.ts | 13 +----------- 7 files changed, 28 insertions(+), 26 deletions(-) create mode 100644 packages/core/auth/src/constants/index.ts diff --git a/packages/core/auth/src/auth.ts b/packages/core/auth/src/auth.ts index 602c5d3acb..57d62bad2e 100644 --- a/packages/core/auth/src/auth.ts +++ b/packages/core/auth/src/auth.ts @@ -10,6 +10,7 @@ import { Context } from '@nocobase/actions'; import { Model } from '@nocobase/database'; import { Authenticator } from './auth-manager'; +import { AuthErrorCode } from './constants'; export type AuthConfig = { authenticator: Authenticator; options: { @@ -17,17 +18,6 @@ export type AuthConfig = { }; ctx: Context; }; -export const AuthErrorCode = { - EMPTY_TOKEN: 'EMPTY_TOKEN' as const, - EXPIRED_TOKEN: 'EXPIRED_TOKEN' as const, - INVALID_TOKEN: 'INVALID_TOKEN' as const, - TOKEN_RENEW_FAILED: 'TOKEN_RENEW_FAILED' as const, - BLOCKED_TOKEN: 'BLOCKED_TOKEN' as const, - EXPIRED_SESSION: 'EXPIRED_SESSION' as const, - NOT_EXIST_USER: 'NOT_EXIST_USER' as const, - SKIP_TOKEN_RENEW: 'SKIP_TOKEN_RENEW' as const, - USER_HAS_NO_ROLES_ERR: 'USER_HAS_NO_ROLES_ERR' as const, -}; export type AuthErrorType = keyof typeof AuthErrorCode; diff --git a/packages/core/auth/src/base/auth.ts b/packages/core/auth/src/base/auth.ts index 566a8e52df..913fcd5615 100644 --- a/packages/core/auth/src/base/auth.ts +++ b/packages/core/auth/src/base/auth.ts @@ -10,7 +10,8 @@ import { Cache } from '@nocobase/cache'; import { Collection, Model } from '@nocobase/database'; import jwt from 'jsonwebtoken'; -import { Auth, AuthConfig, AuthError, AuthErrorCode } from '../auth'; +import { AuthErrorCode } from '../constants'; +import { Auth, AuthConfig, AuthError } from '../auth'; import { JwtService } from './jwt-service'; import { ITokenControlService } from './token-control-service'; diff --git a/packages/core/auth/src/client.ts b/packages/core/auth/src/client.ts index f897fea0b7..712703c0b4 100644 --- a/packages/core/auth/src/client.ts +++ b/packages/core/auth/src/client.ts @@ -7,4 +7,4 @@ * For more information, please refer to: https://www.nocobase.com/agreement. */ -export { AuthErrorCode } from './auth'; +export { AuthErrorCode } from './constants'; diff --git a/packages/core/auth/src/constants/index.ts b/packages/core/auth/src/constants/index.ts new file mode 100644 index 0000000000..af4d347da4 --- /dev/null +++ b/packages/core/auth/src/constants/index.ts @@ -0,0 +1,20 @@ +/** + * This file is part of the NocoBase (R) project. + * Copyright (c) 2020-2024 NocoBase Co., Ltd. + * Authors: NocoBase Team. + * + * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License. + * For more information, please refer to: https://www.nocobase.com/agreement. + */ + +export const AuthErrorCode = { + EMPTY_TOKEN: 'EMPTY_TOKEN' as const, + EXPIRED_TOKEN: 'EXPIRED_TOKEN' as const, + INVALID_TOKEN: 'INVALID_TOKEN' as const, + TOKEN_RENEW_FAILED: 'TOKEN_RENEW_FAILED' as const, + BLOCKED_TOKEN: 'BLOCKED_TOKEN' as const, + EXPIRED_SESSION: 'EXPIRED_SESSION' as const, + NOT_EXIST_USER: 'NOT_EXIST_USER' as const, + SKIP_TOKEN_RENEW: 'SKIP_TOKEN_RENEW' as const, + USER_HAS_NO_ROLES_ERR: 'USER_HAS_NO_ROLES_ERR' as const, +}; diff --git a/packages/core/auth/src/index.ts b/packages/core/auth/src/index.ts index 44390e8c80..62c1edbff4 100644 --- a/packages/core/auth/src/index.ts +++ b/packages/core/auth/src/index.ts @@ -7,6 +7,7 @@ * For more information, please refer to: https://www.nocobase.com/agreement. */ +export { AuthErrorCode } from './constants'; export * from './actions'; export * from './auth'; export * from './auth-manager'; diff --git a/packages/core/client/src/nocobase-buildin-plugin/index.tsx b/packages/core/client/src/nocobase-buildin-plugin/index.tsx index b9d61e9123..7524e0b89f 100644 --- a/packages/core/client/src/nocobase-buildin-plugin/index.tsx +++ b/packages/core/client/src/nocobase-buildin-plugin/index.tsx @@ -12,6 +12,7 @@ import { css } from '@emotion/css'; import { observer } from '@formily/reactive-react'; import { getSubAppName } from '@nocobase/sdk'; import { tval } from '@nocobase/utils/client'; +import { AuthErrorCode } from '@nocobase/auth/client'; import { Button, Modal, Result, Spin } from 'antd'; import React, { FC } from 'react'; import { Navigate, useNavigate } from 'react-router-dom'; @@ -47,7 +48,7 @@ const useErrorProps = (app: Application, error: any) => { const err = error?.response?.data?.errors?.[0] || error; const subApp = getSubAppName(app.getPublicPath()); switch (err.code) { - case 'USER_HAS_NO_ROLES_ERR': + case AuthErrorCode.USER_HAS_NO_ROLES_ERR: return { title: app.i18n.t('Permission denied'), subTitle: err.message, diff --git a/packages/plugins/@nocobase/plugin-auth/src/client/interceptors.ts b/packages/plugins/@nocobase/plugin-auth/src/client/interceptors.ts index 68d6a33653..f96b5011c1 100644 --- a/packages/plugins/@nocobase/plugin-auth/src/client/interceptors.ts +++ b/packages/plugins/@nocobase/plugin-auth/src/client/interceptors.ts @@ -10,18 +10,7 @@ import { Application } from '@nocobase/client'; import type { AxiosResponse } from 'axios'; import debounce from 'lodash/debounce'; - -const AuthErrorCode = { - EMPTY_TOKEN: 'EMPTY_TOKEN' as const, - EXPIRED_TOKEN: 'EXPIRED_TOKEN' as const, - INVALID_TOKEN: 'INVALID_TOKEN' as const, - TOKEN_RENEW_FAILED: 'TOKEN_RENEW_FAILED' as const, - BLOCKED_TOKEN: 'BLOCKED_TOKEN' as const, - EXPIRED_SESSION: 'EXPIRED_SESSION' as const, - NOT_EXIST_USER: 'NOT_EXIST_USER' as const, - SKIP_TOKEN_RENEW: 'SKIP_TOKEN_RENEW' as const, - USER_HAS_NO_ROLES_ERR: 'USER_HAS_NO_ROLES_ERR' as const, -}; +import { AuthErrorCode } from '@nocobase/auth/client'; function removeBasename(pathname, basename) { // Escape special characters in basename for use in regex