import { Injectable, UnauthorizedException } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { JwtService } from '@nestjs/jwt'; import { PassportStrategy } from '@nestjs/passport'; import { ExtractJwt, Strategy } from 'passport-jwt'; import { AccountRepo } from 'src/modules/account/account.repo'; @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { constructor( private readonly accountRepo: AccountRepo , private readonly configService: ConfigService , private readonly jwtService: JwtService ) { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), ignoreExpiration: false, secretOrKey: configService.get('JWT_SECRET')! }); } async validate(payload: any) { const account = await this.accountRepo.findById(payload.id); if (!account || account.length < 1) throw new UnauthorizedException(); return account[0]; } }