Complete the code to import the JWT strategy class from Passport.
import { [1] } from '@nestjs/passport';
The JWT strategy class extends PassportStrategy, so you import PassportStrategy from @nestjs/passport.
Complete the code to extract the JWT token from the request header.
super({ jwtFromRequest: ExtractJwt.[1](), secretOrKey: secret });The JWT token is usually sent in the Authorization header as a Bearer token, so use fromAuthHeaderAsBearerToken().
Fix the error in the validate method to correctly return the user payload.
async validate(payload: any) {
return [1];
}The validate method should return the entire payload object which represents the user info extracted from the token.
Fill both blanks to correctly define the JWT strategy class extending PassportStrategy.
export class JwtStrategy extends [1](Strategy) { constructor() { super({ jwtFromRequest: ExtractJwt.[2](), secretOrKey: process.env.JWT_SECRET, }); } }
The class extends PassportStrategy with the JWT Strategy. The token is extracted using fromAuthHeaderAsBearerToken().
Fill all three blanks to create a validate method that returns user info from the payload.
async validate(payload: any) {
const user = {
id: payload.[1],
username: payload.[2],
email: payload.[3]
};
return user;
}The JWT payload usually has sub for user id, and username and email fields for user info.