import { Injectable } from '@nestjs/common'; import { PassportStrategy } from '@nestjs/passport'; import { ExtractJwt, Strategy } from 'passport-jwt'; import { ConfigService } from '@nestjs/config'; interface JwtPayload { sub: string; email: string; role: string; /** Present on impersonation tokens: the acting admin. */ act?: { sub: string; role: string }; } @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { constructor(configService: ConfigService) { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), ignoreExpiration: false, secretOrKey: configService.getOrThrow('jwt.secret'), }); } async validate(payload: JwtPayload) { return { id: payload.sub, email: payload.email, role: payload.role, // Non-null only while an admin is impersonating this user. impersonatedBy: payload.act?.sub ?? null, impersonatorRole: payload.act?.role ?? null, }; } }