Skip to content

Instantly share code, notes, and snippets.

@jacobdo2
Created June 19, 2020 11:47
Show Gist options
  • Save jacobdo2/fd7a7d06ca24dd146ebe9feaaccb3ceb to your computer and use it in GitHub Desktop.
Save jacobdo2/fd7a7d06ca24dd146ebe9feaaccb3ceb to your computer and use it in GitHub Desktop.
import { ExtractJwt, Strategy } from 'passport-jwt';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable } from '@nestjs/common';
import { AuthService } from './auth.service';
import { passportJwtSecret } from 'jwks-rsa';
import { AuthConfig } from './auth.config';
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(
private readonly authService: AuthService,
private authConfig: AuthConfig,
) {
super({
secretOrKeyProvider: passportJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: `${authConfig.authority}/.well-known/jwks.json`,
}),
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
audience: authConfig.clientId,
issuer: authConfig.authority,
algorithms: ['RS256'],
});
}
public async validate(payload: any) {
return !!payload.sub;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment