import { CanActivate , ExecutionContext , Injectable } from '@nestjs/common' ; import { Reflector } from '@nestjs/core' ; import { AuthService } from './auth.service' ; import { TokenRequirementsHelper } from './token-requirements.decorator' ; @ Injectable ( ) export class TokenGuard implements CanActivate { constructor ( private readonly reflector , private readonly authService ) { } public async canActivate ( context ) { const tokenRequirements = this . reflector . get < TokenRequirementsHelper > ( 'tokenrequirements' , context . getHandler ( ) ) ; if ( ! tokenRequirements ) { return true ; } else { const req = context . switchToHttp ( ) . getRequest ( ) ; if ( req . headers . authorization && ( req . headers . authorization as string ) . split ( ' ' ) [ 0 ] === 'Bearer' ) { try { const token = ( req . headers . authorization as string ) . split ( ' ' ) [ 1 ] ; const decodedToken = await this . authService . validateAccessToken ( token ) ; if ( ! tokenRequirements . tokenIsOfType ( decodedToken . type ) ) return false ; req . token = decodedToken ; return true ; } catch ( err ) { return false ; } } else { return false ; } } } }