Bird
0
0

Given the following code snippet in a NestJS service, what will be the output if the token is valid?

medium📝 component behavior Q13 of 15
NestJS - Authentication
Given the following code snippet in a NestJS service, what will be the output if the token is valid?
const token = jwt.sign({ userId: 123 }, 'secretKey', { expiresIn: '1h' });
const decoded = jwt.verify(token, 'secretKey');
console.log(decoded.userId);
Aundefined
Bnull
C123
DThrows an error
Step-by-Step Solution
Solution:
  1. Step 1: Understand token creation and verification

    The token is created with payload { userId: 123 } and signed with 'secretKey'.
  2. Step 2: Verify token with the same secret

    Verification returns the decoded payload, so decoded.userId is 123.
  3. Final Answer:

    123 -> Option C
  4. Quick Check:

    Valid token returns payload userId = 123 [OK]
Quick Trick: Verify returns original payload if token and secret match [OK]
Common Mistakes:
  • Expecting undefined or null instead of payload
  • Using wrong secret causing error
  • Confusing verify with sign

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions