From 76edc52a724cee51cc82f3fc5e92422071e57cc4 Mon Sep 17 00:00:00 2001 From: Sheldon Guo Date: Mon, 10 Mar 2025 16:21:11 +0800 Subject: [PATCH] feat: add USER_HAS_NO_ROLES_ERR to AuthErrorCode and update related error handling --- packages/core/auth/src/auth.ts | 1 + .../plugin-acl/src/server/middlewares/setCurrentRole.ts | 3 ++- .../plugins/@nocobase/plugin-auth/src/client/interceptors.ts | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/core/auth/src/auth.ts b/packages/core/auth/src/auth.ts index ce150f3378..602c5d3acb 100644 --- a/packages/core/auth/src/auth.ts +++ b/packages/core/auth/src/auth.ts @@ -26,6 +26,7 @@ export const AuthErrorCode = { 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/plugins/@nocobase/plugin-acl/src/server/middlewares/setCurrentRole.ts b/packages/plugins/@nocobase/plugin-acl/src/server/middlewares/setCurrentRole.ts index abe3f416f2..f980a883b5 100644 --- a/packages/plugins/@nocobase/plugin-acl/src/server/middlewares/setCurrentRole.ts +++ b/packages/plugins/@nocobase/plugin-acl/src/server/middlewares/setCurrentRole.ts @@ -10,6 +10,7 @@ import { Context } from '@nocobase/actions'; import { Cache } from '@nocobase/cache'; import { Model, Repository } from '@nocobase/database'; +import { AuthErrorCode } from '@nocobase/auth'; export async function setCurrentRole(ctx: Context, next) { const currentRole = ctx.get('X-Role'); @@ -52,7 +53,7 @@ export async function setCurrentRole(ctx: Context, next) { role = userRoles.find((role) => role.name === currentRole)?.name; if (!role) { return ctx.throw(401, { - code: 'ROLE_NOT_FOUND_FOR_USER', + code: AuthErrorCode.USER_HAS_NO_ROLES_ERR, message: ctx.t('The role does not belong to the user', { ns: 'acl' }), }); } diff --git a/packages/plugins/@nocobase/plugin-auth/src/client/interceptors.ts b/packages/plugins/@nocobase/plugin-auth/src/client/interceptors.ts index d1ad83715c..68d6a33653 100644 --- a/packages/plugins/@nocobase/plugin-auth/src/client/interceptors.ts +++ b/packages/plugins/@nocobase/plugin-auth/src/client/interceptors.ts @@ -68,7 +68,7 @@ export function authCheckMiddleware({ app }: { app: Application }) { error.config.skipNotify = false; } - if (firstError?.code === 'USER_HAS_NO_ROLES_ERR') { + if (firstError?.code === AuthErrorCode.USER_HAS_NO_ROLES_ERR) { // use app error to show error message error.config.skipNotify = true; app.error = firstError;