fix: auth & role code

This commit is contained in:
puriphat 2024-06-14 06:30:10 +00:00
parent 1048944773
commit be46aa3cff
2 changed files with 4 additions and 4 deletions

View file

@ -12,6 +12,6 @@ export async function expressAuthentication(
case "keycloak": case "keycloak":
return keycloakAuth(request, scopes); return keycloakAuth(request, scopes);
default: default:
throw new HttpError(HttpStatus.NOT_IMPLEMENTED, "ไม่ทราบวิธียืนยันตัวตน"); throw new HttpError(HttpStatus.NOT_IMPLEMENTED, "Unknown how to verify identity.", "unknowHowToVerify");
} }
} }

View file

@ -5,14 +5,14 @@ import HttpStatus from "../interfaces/http-status";
export function role( export function role(
role: string | string[], role: string | string[],
errorMessage: string = "คุณไม่มีสิทธิในการเข้าถึงทรัพยากรดังกล่าว", errorMessage: string = "You do not have permission to access this resource.",
) { ) {
return (req: RequestWithUser, _res: Response, next: NextFunction) => { return (req: RequestWithUser, _res: Response, next: NextFunction) => {
if (!Array.isArray(role) && !req.user.role.includes(role) && !req.user.role.includes("*")) { if (!Array.isArray(role) && !req.user.role.includes(role) && !req.user.role.includes("*")) {
throw new HttpError(HttpStatus.FORBIDDEN, errorMessage); throw new HttpError(HttpStatus.FORBIDDEN, errorMessage, "noPermissionToAccess");
} }
if (role !== "*" && !req.user.role.some((v) => role.includes(v))) { if (role !== "*" && !req.user.role.some((v) => role.includes(v))) {
throw new HttpError(HttpStatus.FORBIDDEN, errorMessage); throw new HttpError(HttpStatus.FORBIDDEN, errorMessage, "noPermissionToAccess");
} }
return next(); return next();
}; };