| 1234567891011121314151617181920212223 |
- import { Injectable, UnauthorizedException } from '@nestjs/common';
- import { PassportStrategy } from '@nestjs/passport';
- import { ExtractJwt, Strategy } from 'passport-jwt';
- import { JwtPayload } from './auth-types';
- import { AuthService, JWT_SECRET } from './auth.service';
- @Injectable()
- export class JwtStrategy extends PassportStrategy(Strategy) {
- constructor(private readonly authService: AuthService) {
- super({
- jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
- secretOrKey: JWT_SECRET,
- });
- }
- async validate(payload: JwtPayload, done: (err: Error, user: any) => void) {
- const user = await this.authService.validateUser(payload);
- if (!user) {
- return done(new UnauthorizedException(), false);
- }
- done(null, user);
- }
- }
|