0
0
NestJSframework~20 mins

JWT strategy in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JWT Strategy Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when a valid JWT token is provided?
Consider a NestJS JWT strategy that validates a token and returns the user payload. What will be the result of the validate method if the token is valid and contains { userId: 42, username: 'alice' }?
NestJS
async validate(payload: any) {
  return { id: payload.userId, name: payload.username };
}
AThrows UnauthorizedException
B{ userId: 42, username: 'alice' }
C{ id: 42, name: 'alice' }
DReturns null
Attempts:
2 left
💡 Hint
The validate method transforms the JWT payload into a user object.
📝 Syntax
intermediate
2:00remaining
Which option correctly imports and uses Passport JWT strategy in NestJS?
You want to create a JWT strategy in NestJS using Passport. Which import and class extension is correct?
A
import { JwtStrategy } from '@nestjs/passport';
export class JwtStrategy extends JwtStrategy {}
B
import { PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-jwt';
export class JwtStrategy extends PassportStrategy(Strategy) {}
C
import { Strategy } from 'passport-jwt';
export class JwtStrategy extends Strategy {}
D
import { PassportJwtStrategy } from 'passport';
export class JwtStrategy extends PassportJwtStrategy {}
Attempts:
2 left
💡 Hint
NestJS wraps Passport strategies with PassportStrategy class.
🔧 Debug
advanced
2:00remaining
Why does the JWT strategy always throw UnauthorizedException?
Given this JWT strategy code snippet, why does authentication always fail with UnauthorizedException? class JwtStrategy extends PassportStrategy(Strategy) { constructor() { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), secretOrKey: 'secretKey', }); } async validate(payload) { return null; } }
AThe constructor is missing a call to super() with options.
BThe secret key is incorrect, causing token verification to fail.
CThe jwtFromRequest function is not extracting the token properly.
DThe validate method returns null, which Passport treats as failed validation.
Attempts:
2 left
💡 Hint
The validate method must return a user object or throw an exception.
🧠 Conceptual
advanced
2:00remaining
What is the purpose of the secretOrKey option in JWT strategy?
In NestJS JWT strategy configuration, what does the secretOrKey option do?
AIt specifies the secret used to verify the JWT signature.
BIt defines the expiration time of the JWT token.
CIt sets the algorithm used to sign the JWT token.
DIt extracts the JWT token from the request headers.
Attempts:
2 left
💡 Hint
Think about how JWT tokens are verified.
state_output
expert
2:00remaining
What is the value of request.user after successful JWT authentication?
In a NestJS controller guarded by JwtAuthGuard, after a successful request with a valid JWT token, what will request.user contain?
AThe object returned by the JWT strategy's validate method.
BAn empty object {}.
CThe raw JWT token string from the Authorization header.
DThe decoded JWT payload without any transformation.
Attempts:
2 left
💡 Hint
The validate method controls what user data is attached to the request.