import { Injectable, type ExecutionContext } from '@nestjs/common'; import { AuthGuard } from '@nestjs/passport'; /** * JWT guard that does NOT throw when the token is absent or invalid. * When no valid token is provided, `request.user` is left as `undefined`. * Use this for endpoints that are public but can serve richer data to * authenticated callers (e.g. listing detail with access-gated fields). */ @Injectable() export class OptionalJwtAuthGuard extends AuthGuard('jwt') { override canActivate(context: ExecutionContext) { return super.canActivate(context); } // eslint-disable-next-line @typescript-eslint/no-explicit-any override handleRequest(_err: unknown, user: TUser): TUser { // Return whatever passport resolved (may be false/undefined for anonymous requests) return user; } }