Bird
0
0

What will be the output of the following code if the token is valid?

medium📝 Predict Output Q5 of 15
NestJS - Authentication
What will be the output of the following code if the token is valid?
const payload = jwtService.decode(token);
return payload.sub;
AUser ID from token's subject claim
BThrows an error
CUndefined
DBoolean true
Step-by-Step Solution
Solution:
  1. Step 1: Understand jwtService.decode behavior

    decode returns the decoded token payload without verifying signature.
  2. Step 2: Access the 'sub' claim from payload

    payload.sub typically contains the user ID or subject identifier.
  3. Final Answer:

    User ID from token's subject claim -> Option A
  4. Quick Check:

    decode() returns payload, payload.sub = user ID [OK]
Quick Trick: decode() reads token data without validation [OK]
Common Mistakes:
  • Expecting decode to verify token
  • Assuming decode throws error
  • Confusing decode with verify

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions