import { HttpException , HttpStatus , Injectable } from '@nestjs/common' ; import { PassportStrategy } from '@nestjs/passport' ; import { ExtractJwt , Strategy , VerifiedCallback } from 'passport-jwt' ; import { AuthService } from './auth.service' ; @ Injectable ( ) export class JwtStrategy extends PassportStrategy ( Strategy ) { constructor ( private authService ) { super ( { jwtFromRequest : ExtractJwt . fromAuthHeaderAsBearerToken ( ) , secretOrKey : process . env . SECRET_KEY , } ) ; } async validate ( payload , done ) { const user = await this . authService . validateUser ( payload ) ; if ( ! user ) { return done ( new HttpException ( 'Unauthorized access' , HttpStatus . UNAUTHORIZED ) , false ) ; } return done ( null , user , payload . iat ) ; } }