import { Injectable, UnauthorizedException } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { PassportStrategy } from '@nestjs/passport'; import { ExtractJwt, Strategy } from 'passport-jwt'; import { UserService } from '~/user/services'; import { IJwtPayload } from '../interfaces'; @Injectable() export class AccessTokenStrategy extends PassportStrategy(Strategy, 'access-token') { constructor(configService: ConfigService, private userService: UserService) { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), ignoreExpiration: false, secretOrKey: configService.get('JWT_ACCESS_TOKEN_SECRET'), }); } async validate(payload: IJwtPayload) { const user = await this.userService.findUser({ id: payload.sub }); if (!user) { throw new UnauthorizedException(); } return payload; } }