import { Injectable , UnauthorizedException } from '@nestjs/common' ; import { ExtractJwt , Strategy } from 'passport-jwt' ; import { AuthService } from '../auth.service' ; import { PassportStrategy } from '@nestjs/passport' ; import { JwtPayload } from '../interfaces/jwt-payload.interface' ; import 'dotenv/config' ; @ Injectable ( ) export class JwtStrategy extends PassportStrategy ( Strategy ) { constructor ( private authService ) { super ( { jwtFromRequest : ExtractJwt . fromAuthHeaderAsBearerToken ( ) , ignoreExpiration : false , secretOrKey : process . env . SECRET_KEY , } ) ; } async validate ( payload ) { const user = await this . authService . validateUserByJwt ( payload ) ; if ( ! user ) { throw new UnauthorizedException ( ) ; } return user ; } }