mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-07-01 10:42:19 +08:00
feat: add USER_HAS_NO_ROLES_ERR to AuthErrorCode and update related error handling
This commit is contained in:
parent
b5b142e1e2
commit
76edc52a72
@ -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;
|
||||
|
@ -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' }),
|
||||
});
|
||||
}
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user