import { Injectable , UnauthorizedException } from '@nestjs/common' ; import { PassportStrategy } from '@nestjs/passport' ; import { ExtractJwt , Strategy } from 'passport-jwt' ; import { ConfigService } from '../config/config.service' ; import { AuthService } from './auth.service' ; import { ObjectID } from 'typeorm/driver/mongodb/typings' ; import { LoginPayload } from './login.payload' ; import { JwtPayload } from './jwt.payload' ; @ Injectable ( ) export class JwtStrategy extends PassportStrategy ( Strategy ) { constructor ( private readonly authService , private readonly configService ) { super ( { jwtFromRequest : ExtractJwt . fromExtractors ( [ ExtractJwt . fromAuthHeaderAsBearerToken ( ) , ExtractJwt . fromUrlQueryParameter ( 'access_token' ) , ] ) , secretOrKey : configService . get ( 'JWT_SECRET' ) , } ) ; } async validate ( payload ) { const user = await this . authService . validateUser ( payload ) ; if ( ! user ) { throw new UnauthorizedException ( ) ; } return user ; } }