import { Injectable, Logger } from '@nestjs/common'; import { PassportStrategy } from '@nestjs/passport'; import { Strategy as JwtStrategyBase, ExtractJwt } from 'passport-jwt'; import { ConfigService } from '@nestjs/config'; function cookieExtractor(req: any): string | null { if (!req) return null; if (req.cookies && req.cookies.raylab_jwt) return req.cookies.raylab_jwt as string; return null; } @Injectable() export class JwtStrategy extends PassportStrategy(JwtStrategyBase, 'jwt') { private readonly logger = new Logger('JwtStrategy'); constructor(private config: ConfigService) { super({ jwtFromRequest: ExtractJwt.fromExtractors([cookieExtractor, ExtractJwt.fromAuthHeaderAsBearerToken()]), secretOrKey: config.get('RAYLAB_JWT_SECRET') || process.env.RAYLAB_JWT_SECRET || 'raylab-secret', algorithms: ['HS256'], }); this.logger.debug('JwtStrategy initialized'); } async validate(payload: any) { this.logger.debug(`JwtStrategy.validate payload=${JSON.stringify(payload)}`); return payload; // attached to req.user } }