import { Injectable , UnauthorizedException } from '@nestjs/common' ; import { PassportStrategy } from '@nestjs/passport' ; import { ExtractJwt , Strategy } from 'passport-jwt' ; import type { RoleType } from '../../constants' ; import { TokenType } from '../../constants' ; import { ApiConfigService } from '../../shared/services/api-config.service' ; import type { UserEntity } from '../user/user.entity' ; import { UserService } from '../user/user.service' ; @ Injectable ( ) export class JwtStrategy extends PassportStrategy ( Strategy ) { constructor ( private configService , private userService ) { super ( { jwtFromRequest : ExtractJwt . fromAuthHeaderAsBearerToken ( ) , secretOrKey : configService . authConfig . jwtSecret , } ) ; } async validate ( args ) { if ( args . type !== TokenType . ACCESS_TOKEN ) { throw new UnauthorizedException ( ) ; } const user = await this . userService . findOne ( { id : args . userId , role : args . role , } ) ; if ( ! user ) { throw new UnauthorizedException ( ) ; } return user ; } }