import { Injectable , CanActivate , ExecutionContext , UnauthorizedException , } from '@nestjs/common' ; @ Injectable ( ) export class RolesGuard implements CanActivate { private roles = [ 'admin' ] ; constructor ( ) { } canActivate ( context ) { const request = context . switchToHttp ( ) . getRequest ( ) ; const user = request . user ; if ( user . roles . filter ( ( role ) => this . roles . includes ( role ) ) . length ) { return true ; } throw new UnauthorizedException ( ) ; } }