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" ; import { ConfigService } from "nestx-config" ; @ Injectable ( ) export class JwtStrategy extends PassportStrategy ( Strategy ) { constructor ( private readonly authService , configService ) { super ( { jwtFromRequest : ExtractJwt . fromExtractors ( [ ExtractJwt . fromAuthHeaderAsBearerToken ( ) , ExtractJwt . fromUrlQueryParameter ( "access_token" ) , req => { const token = req . cookies && req . cookies . access_token ; return token ; } ] ) , secretOrKey : configService . get ( "JWT_SECRET_KEY" ) } ) ; } async validate ( payload ) { const user = await this . authService . validateUser ( payload ) ; if ( ! user ) { throw new UnauthorizedException ( ) ; } return user ; } }