import { Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { PassportStrategy } from '@nestjs/passport'; import { ExtractJwt, Strategy } from 'passport-jwt'; import { UserDocument } from '../../schema/user.schema'; import { UserService } from '../user/user.service'; @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { constructor( configService: ConfigService, private readonly userService: UserService, ) { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), ignoreExpiration: false, secretOrKey: configService.get('AUTH_SECRET'), }); } async validate(payload: any): Promise { try { return await this.userService.findById(payload.sub) } catch (e) { // log error } return null } }