import { Injectable , UnauthorizedException } from '@nestjs/common' ; import { PassportStrategy } from '@nestjs/passport' ; import { ExtractJwt , Strategy } from 'passport-jwt' ; import { jwtConstants } from '../constants' ; import { UserService } from '../../user/user.service' ; @ Injectable ( ) export class JwtStrategy extends PassportStrategy ( Strategy ) { constructor ( private userService ) { super ( { jwtFromRequest : ExtractJwt . fromAuthHeaderAsBearerToken ( ) , ignoreExpiration : false , secretOrKey : jwtConstants . secret , } ) ; } async validate ( payload ) { const existUser = this . userService . findOne ( payload . sub ) ; if ( ! existUser ) { throw new UnauthorizedException ( ) ; } return { ... payload , id : payload . sub } ; } }