You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
809 B
26 lines
809 B
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
|
import { AuthService, JwtPayload } from './auth.service';
|
|
import { PassportStrategy } from '@nestjs/passport';
|
|
import { ExtractJwt, Strategy } from 'passport-jwt';
|
|
import { ConfigService } from '../config/config.service';
|
|
|
|
@Injectable()
|
|
export class JwtStrategy extends PassportStrategy(Strategy) {
|
|
constructor(
|
|
private readonly authService: AuthService,
|
|
private readonly configService: ConfigService,
|
|
) {
|
|
super({
|
|
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
|
secretOrKey: configService.jwtSecret,
|
|
});
|
|
}
|
|
|
|
async validate(payload: JwtPayload) {
|
|
const user = await this.authService.validateUser(payload);
|
|
if (!user) {
|
|
throw new UnauthorizedException();
|
|
}
|
|
return user;
|
|
}
|
|
}
|