import { Injectable, ExecutionContext } from '@nestjs/common'; import { Reflector } from '@nestjs/core'; import { AuthGuard } from '@nestjs/passport'; import { Observable } from 'rxjs'; import { IS_PUBLIC_KEY } from 'src/shared/decorators/public.decorator'; import { LOG_TO_DISCORD } from 'src/shared/decorators/log-to-discord.decorator'; import { SHOULD_LOG_TO_DISCORD } from 'src/shared/modules/discord-logger/discord-logger.filter'; @Injectable() export class UserAuthGuard extends AuthGuard('user-jwt') { constructor(private reflector: Reflector) { super(); } canActivate( context: ExecutionContext, ): boolean | Promise | Observable { // Check if route should be logged to Discord const shouldLogToDiscord = this.reflector.get(LOG_TO_DISCORD, context.getHandler()) || this.reflector.get(LOG_TO_DISCORD, context.getClass()); // Mark the request if it should be logged if (shouldLogToDiscord) { const request = context.switchToHttp().getRequest(); (request as any)[SHOULD_LOG_TO_DISCORD] = true; } // Check if route is public const isPublic = this.reflector.getAllAndOverride(IS_PUBLIC_KEY, [ context.getHandler(), context.getClass(), ]); if (isPublic) { return true; } return super.canActivate(context); } }