import { CanActivate , ExecutionContext , Injectable } from '@nestjs/common' ; import { Reflector } from '@nestjs/core' ; import { Role } from '../../roles/roles.interface' ; import { ROLES_KEY } from '../../roles/roles.decorator' ; import { UserService } from '../../user/user.service' ; @ Injectable ( ) export class RolesGuard implements CanActivate { constructor ( private reflector , private userService ) { } async canActivate ( context ) { const requiredRoles = this . reflector . getAllAndOverride < Role [ ] > ( ROLES_KEY , [ context . getHandler ( ) , context . getClass ( ) , ] ) ; if ( ! requiredRoles ) { return true ; } const { user } = context . switchToHttp ( ) . getRequest ( ) ; const adminUser = await this . userService . checkAdmin ( user . id ) ; return ! ! adminUser ; } }