import { CanActivate , ExecutionContext , Injectable } from '@nestjs/common' ; import { Reflector } from '@nestjs/core' ; import { GqlContextType , GqlExecutionContext } from '@nestjs/graphql' ; import type { Request } from 'express' ; @ Injectable ( ) export class AuthenticatedGuard implements CanActivate { constructor ( private reflector ) { } public canActivate ( context ) { const isPublic = this . reflector . get < boolean > ( 'isPublic' , context . getHandler ( ) ) ; if ( isPublic ) { return true ; } const request = this . getRequest ( context ) ; return request . isAuthenticated ( ) ; } public getRequest ( context ) { if ( context . getType < GqlContextType > ( ) === 'graphql' ) { const ctx = GqlExecutionContext . create ( context ) . getContext ( ) ; return < Request > ctx . req ; } return context . switchToHttp ( ) . getRequest < Request > ( ) ; } }