| 8 | |
| 9 | @Injectable() |
| 10 | export class AdminGuard implements CanActivate { |
| 11 | constructor(private prismaService: PrismaService) {} |
| 12 | async validateAdmin(userId: string) { |
| 13 | const user = await this.prismaService.users.findUnique({ |
| 14 | where: { |
| 15 | id: userId, |
| 16 | }, |
| 17 | }); |
| 18 | if (user?.role === 'admin') { |
| 19 | return true; |
| 20 | } |
| 21 | return false; |
| 22 | } |
| 23 | async canActivate(context: ExecutionContext): Promise<boolean> { |
| 24 | const request = context.switchToHttp().getRequest(); |
| 25 | const user = request.user; |
| 26 | if (user) { |
| 27 | const isAdmin = await this.validateAdmin(user.id); |
| 28 | if (isAdmin) { |
| 29 | return true; |
| 30 | } |
| 31 | } |
| 32 | throw new UnauthorizedException( |
| 33 | 'You are not authorized to access this route.' |
| 34 | ); |
| 35 | } |
| 36 | } |
nothing calls this directly
no outgoing calls
no test coverage detected