MCPcopy Create free account
hub / github.com/bsoc-bitbyte/busify / AdminGuard

Class AdminGuard

backend/src/Auth/utils/admin.guard.ts:10–36  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8
9@Injectable()
10export 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}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected