import { Injectable , CanActivate , ExecutionContext , NotFoundException } from '@nestjs/common' ; import { Reflector } from '@nestjs/core' ; @ Injectable ( ) export class RoleGuard implements CanActivate { constructor ( private readonly reflector ) { } canActivate ( context ) { const roles = this . reflector . get < string [ ] > ( 'role' , context . getHandler ( ) ) ; if ( ! roles ) { return true ; } const request = context . switchToHttp ( ) . getRequest ( ) ; const { user } = request ; const hasRole = ( ) : boolean => roles . includes ( user . role ) ; if ( user && ! hasRole ( ) ) { throw new NotFoundException ( ) ; } return user && user . role && hasRole ( ) ; } }