import { Injectable, UnauthorizedException } from '@nestjs/common'; import { PassportStrategy } from '@nestjs/passport'; import { ExtractJwt, Strategy } from 'passport-jwt'; import { type RoleType, 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( configService: ApiConfigService, private userService: UserService, ) { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), secretOrKey: configService.authConfig.publicKey, }); } async validate(args: { userId: Uuid; role: RoleType; type: TokenType; }): Promise { if (args.type !== TokenType.ACCESS_TOKEN) { throw new UnauthorizedException(); } const user = await this.userService.findOne({ // FIXME: issue with type casts id: args.userId as never, role: args.role, }); if (!user) { throw new UnauthorizedException(); } return user; } }