import { PassportStrategy } from '@nestjs/passport' ; import { Strategy , ExtractJwt } from 'passport-jwt' ; import { Injectable , UnauthorizedException } from '@nestjs/common' ; import { JwtPayload } from '../models/jwt-payload.interface' ; import { Model } from 'mongoose' ; import { User } from '../models/user.model' ; import { InjectModel } from '@nestjs/mongoose' ; @ Injectable ( ) export class JwtStrategy extends PassportStrategy ( Strategy ) { constructor ( @ InjectModel ( 'User' ) private userModel ) { super ( { jwtFromRequest : ExtractJwt . fromAuthHeaderAsBearerToken ( ) , secretOrKey : process . env . JWT_SECRET , } ) ; } async validate ( payload ) { const { email } = payload ; const user = await this . userModel . findOne ( { email } ) ; if ( ! user ) { throw new UnauthorizedException ( ) ; } return user ; } }