import { ExtractJwt , Strategy } from 'passport-jwt' ; import { Injectable , UnauthorizedException } from '@nestjs/common' ; import { LoginService } from '../login.service' ; import { PassportStrategy } from '@nestjs/passport' ; import { JwtPayload } from '../interfaces/jwt.payload' ; @ Injectable ( ) export class JwtStrategy extends PassportStrategy ( Strategy ) { constructor ( private readonly loginService ) { super ( { jwtFromRequest : ExtractJwt . fromAuthHeaderAsBearerToken ( ) , secretOrKey : process . env . SECRET_KEY_JWT , } ) ; } async validate ( payload ) { const user = await this . loginService . validateUserByJwt ( payload ) ; if ( ! user ) { throw new UnauthorizedException ( ) ; } return user ; } }