import { ExecutionContext , Injectable , UnauthorizedException , } from '@nestjs/common' ; import { Reflector } from '@nestjs/core' ; import { AuthGuard } from '@nestjs/passport' ; import { BYPASS_AUTH_KEY } from './bypass-auth.decorator' ; import { getRequest } from './getRequest' ; import { User } from './user' ; @ Injectable ( ) export class IdsUserGuard extends AuthGuard ( 'jwt' ) { getRequest = getRequest ; constructor ( private readonly reflector ) { super ( ) ; } handleRequest < TUser extends User > ( error , user , info , context , status ? ) { super . handleRequest ( error , user , info , context , status ) ; if ( ! user . nationalId ) { throw new UnauthorizedException ( ) ; } const request = this . getRequest ( context ) ; request . auth = user ; return user ; } canActivate ( context ) { const bypassAuth = this . reflector . getAllAndOverride < boolean > ( BYPASS_AUTH_KEY , [ context . getHandler ( ) , context . getClass ( ) ] ) ; if ( bypassAuth ) { return true ; } return super . canActivate ( context ) ; } }