feat: move AuthErrorCode to constants and update imports

This commit is contained in:
Sheldon Guo 2025-03-10 16:52:08 +08:00
parent 76edc52a72
commit e4e508ac8e
7 changed files with 28 additions and 26 deletions

View File

@ -10,6 +10,7 @@
import { Context } from '@nocobase/actions'; import { Context } from '@nocobase/actions';
import { Model } from '@nocobase/database'; import { Model } from '@nocobase/database';
import { Authenticator } from './auth-manager'; import { Authenticator } from './auth-manager';
import { AuthErrorCode } from './constants';
export type AuthConfig = { export type AuthConfig = {
authenticator: Authenticator; authenticator: Authenticator;
options: { options: {
@ -17,17 +18,6 @@ export type AuthConfig = {
}; };
ctx: Context; 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; export type AuthErrorType = keyof typeof AuthErrorCode;

View File

@ -10,7 +10,8 @@
import { Cache } from '@nocobase/cache'; import { Cache } from '@nocobase/cache';
import { Collection, Model } from '@nocobase/database'; import { Collection, Model } from '@nocobase/database';
import jwt from 'jsonwebtoken'; 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 { JwtService } from './jwt-service';
import { ITokenControlService } from './token-control-service'; import { ITokenControlService } from './token-control-service';

View File

@ -7,4 +7,4 @@
* For more information, please refer to: https://www.nocobase.com/agreement. * For more information, please refer to: https://www.nocobase.com/agreement.
*/ */
export { AuthErrorCode } from './auth'; export { AuthErrorCode } from './constants';

View File

@ -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,
};

View File

@ -7,6 +7,7 @@
* For more information, please refer to: https://www.nocobase.com/agreement. * For more information, please refer to: https://www.nocobase.com/agreement.
*/ */
export { AuthErrorCode } from './constants';
export * from './actions'; export * from './actions';
export * from './auth'; export * from './auth';
export * from './auth-manager'; export * from './auth-manager';

View File

@ -12,6 +12,7 @@ import { css } from '@emotion/css';
import { observer } from '@formily/reactive-react'; import { observer } from '@formily/reactive-react';
import { getSubAppName } from '@nocobase/sdk'; import { getSubAppName } from '@nocobase/sdk';
import { tval } from '@nocobase/utils/client'; import { tval } from '@nocobase/utils/client';
import { AuthErrorCode } from '@nocobase/auth/client';
import { Button, Modal, Result, Spin } from 'antd'; import { Button, Modal, Result, Spin } from 'antd';
import React, { FC } from 'react'; import React, { FC } from 'react';
import { Navigate, useNavigate } from 'react-router-dom'; 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 err = error?.response?.data?.errors?.[0] || error;
const subApp = getSubAppName(app.getPublicPath()); const subApp = getSubAppName(app.getPublicPath());
switch (err.code) { switch (err.code) {
case 'USER_HAS_NO_ROLES_ERR': case AuthErrorCode.USER_HAS_NO_ROLES_ERR:
return { return {
title: app.i18n.t('Permission denied'), title: app.i18n.t('Permission denied'),
subTitle: err.message, subTitle: err.message,

View File

@ -10,18 +10,7 @@
import { Application } from '@nocobase/client'; import { Application } from '@nocobase/client';
import type { AxiosResponse } from 'axios'; import type { AxiosResponse } from 'axios';
import debounce from 'lodash/debounce'; import debounce from 'lodash/debounce';
import { AuthErrorCode } from '@nocobase/auth/client';
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,
};
function removeBasename(pathname, basename) { function removeBasename(pathname, basename) {
// Escape special characters in basename for use in regex // Escape special characters in basename for use in regex