mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-25 00:57:55 +00:00
22 lines
614 B
TypeScript
22 lines
614 B
TypeScript
import { RoleType } from '@app/common/constants/role.type.enum';
|
|
import { BadRequestException, UnauthorizedException } from '@nestjs/common';
|
|
import { AuthGuard } from '@nestjs/passport';
|
|
|
|
export class AdminRoleGuard extends AuthGuard('jwt') {
|
|
handleRequest(err, user) {
|
|
if (err || !user) {
|
|
throw err || new UnauthorizedException();
|
|
} else {
|
|
if (
|
|
!(
|
|
user.role.type === RoleType.ADMIN ||
|
|
user.role.type === RoleType.SUPER_ADMIN
|
|
)
|
|
) {
|
|
throw new BadRequestException('Only admin role can access this route');
|
|
}
|
|
}
|
|
return user;
|
|
}
|
|
}
|