import { Injectable , ExecutionContext , UnauthorizedException } from '@nestjs/common' ; import { AuthGuard } from '@nestjs/passport' ; import { GqlExecutionContext } from '@nestjs/graphql' ; import { decryptValue } from '@utils/crypto' ; import { jwtConstants } from '../constants' ; const setAuth = ( context ) => { const gqlContext = GqlExecutionContext . create ( context ) ; const ctx = gqlContext . getContext ( ) ; const authCookie = ctx . req . cookies [ jwtConstants . header ] ; if ( authCookie ) { ctx . req . headers . authorization = `Bearer ${ decryptValue ( authCookie ) }` ; } return ctx . req ; } ; @ Injectable ( ) export class JwtAuthGuard extends AuthGuard ( 'jwt' ) { constructor ( ) { super ( ) ; } handleRequest ( err , user , info ) { if ( err || ! user ) { throw err || new UnauthorizedException ( info . message ) ; } return user ; } getRequest ( context ) { return setAuth ( context ) ; } } @ Injectable ( ) export class ExpriedJwtAuthGuard extends AuthGuard ( 'jwt-expried' ) { constructor ( ) { super ( ) ; } handleRequest ( err , user ) { return user ; } getRequest ( context ) { return setAuth ( context ) ; } }