import { Injectable, UnauthorizedException } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { PassportStrategy } from '@nestjs/passport'; import { ExtractJwt, Strategy } from 'passport-jwt'; import { HttpErrorCode } from '../../../common/error-code'; import { Environment } from '../../../config/env.validation'; import { AccessPayload } from '../application/auth.types'; @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { constructor(config: ConfigService) { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), ignoreExpiration: false, secretOrKey: config.getOrThrow('JWT_SECRET'), algorithms: ['HS256'], }); } validate(payload: AccessPayload): AccessPayload { if (payload.type !== 'access') { throw new UnauthorizedException({ code: HttpErrorCode.Unauthorized }); } return payload; } }