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