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