Bird
0
0

Identify the error in this JwtStrategy constructor code snippet:

medium📝 Debug Q14 of 15
NestJS - Authentication
Identify the error in this JwtStrategy constructor code snippet:
constructor(private readonly jwtService: JwtService) {
  super({
    jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
    secretOrKey: process.env.JWT_SECRET,
  });
}
AMissing call to super() with options object
Bprocess.env.JWT_SECRET might be undefined causing runtime error
CjwtFromRequest should be a function, not a method call
DJwtService should not be injected in JwtStrategy constructor
Step-by-Step Solution
Solution:
  1. Step 1: Review constructor usage

    The constructor correctly calls super() with options including jwtFromRequest and secretOrKey.
  2. Step 2: Check environment variable usage

    If process.env.JWT_SECRET is undefined, the strategy will fail at runtime because the secret key is missing.
  3. Final Answer:

    process.env.JWT_SECRET might be undefined causing runtime error -> Option B
  4. Quick Check:

    Missing JWT secret causes runtime failure [OK]
Quick Trick: Always ensure JWT_SECRET env variable is set before running [OK]
Common Mistakes:
  • Assuming jwtFromRequest is incorrectly used
  • Thinking JwtService injection is invalid here
  • Ignoring possibility of missing environment variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes