import { ExtractJwt, Strategy } from 'passport-jwt'; import { Injectable, UnauthorizedException } from '@nestjs/common'; import { PassportStrategy } from '@nestjs/passport'; import { ConfigService } from '@nestjs/config'; import { OrNeverType } from '../../utils/types/or-never.type'; import { AllConfigType } from 'src/config/config.type'; import { JwtPayloadType } from './types/jwt-payload.type'; @Injectable() export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') { constructor(configService: ConfigService) { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), secretOrKey: configService.get('auth.secret', { infer: true }), }); } // Why we don't check if the user exists in the database: // https://github.com/brocoders/nestjs-boilerplate/blob/main/docs/auth.md#about-jwt-strategy public validate(payload: JwtPayloadType): OrNeverType { if (!payload.id) { throw new UnauthorizedException(); } return payload; } }