feat: add USER_HAS_NO_ROLES_ERR to AuthErrorCode and update related error handling

This commit is contained in:
Sheldon Guo 2025-03-10 16:21:11 +08:00
parent b5b142e1e2
commit 76edc52a72
3 changed files with 4 additions and 2 deletions

View File

@ -26,6 +26,7 @@ export const AuthErrorCode = {
EXPIRED_SESSION: 'EXPIRED_SESSION' as const, EXPIRED_SESSION: 'EXPIRED_SESSION' as const,
NOT_EXIST_USER: 'NOT_EXIST_USER' as const, NOT_EXIST_USER: 'NOT_EXIST_USER' as const,
SKIP_TOKEN_RENEW: 'SKIP_TOKEN_RENEW' 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,6 +10,7 @@
import { Context } from '@nocobase/actions'; import { Context } from '@nocobase/actions';
import { Cache } from '@nocobase/cache'; import { Cache } from '@nocobase/cache';
import { Model, Repository } from '@nocobase/database'; import { Model, Repository } from '@nocobase/database';
import { AuthErrorCode } from '@nocobase/auth';
export async function setCurrentRole(ctx: Context, next) { export async function setCurrentRole(ctx: Context, next) {
const currentRole = ctx.get('X-Role'); 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; role = userRoles.find((role) => role.name === currentRole)?.name;
if (!role) { if (!role) {
return ctx.throw(401, { 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' }), message: ctx.t('The role does not belong to the user', { ns: 'acl' }),
}); });
} }

View File

@ -68,7 +68,7 @@ export function authCheckMiddleware({ app }: { app: Application }) {
error.config.skipNotify = false; 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 // use app error to show error message
error.config.skipNotify = true; error.config.skipNotify = true;
app.error = firstError; app.error = firstError;