Bird
0
0

Given this JwtStrategy validate method:

medium📝 state output Q4 of 15
NestJS - Authentication
Given this JwtStrategy validate method:
async validate(payload: any) {
  return { userId: payload.sub, username: payload.username };
}
What will be the returned object if the token payload is { sub: 10, username: 'alice' }?
Aundefined
B{ userId: 'alice', username: 10 }
C{ sub: 10, username: 'alice' }
D{ userId: 10, username: 'alice' }
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the validate method return

    The method returns an object with userId set to payload.sub and username from payload.username.
  2. Step 2: Substitute payload values

    Given payload.sub = 10 and payload.username = 'alice', the returned object is { userId: 10, username: 'alice' }.
  3. Final Answer:

    { userId: 10, username: 'alice' } -> Option D
  4. Quick Check:

    Payload mapping = Correct object [OK]
Quick Trick: Map payload.sub to userId in validate() [OK]
Common Mistakes:
  • Mixing up keys userId and sub
  • Returning payload directly without mapping

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes