0
0
Node.jsframework~10 mins

JWT token generation and verification in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the JWT library.

Node.js
const jwt = require('[1]');
Drag options to blanks, or click blank then click option'
Ajsonwebtoken
Bexpress
Chttp
Dcrypto
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express' instead of 'jsonwebtoken'.
Using 'crypto' which is for cryptography but not JWT.
2fill in blank
medium

Complete the code to generate a JWT token with a secret key.

Node.js
const token = jwt.sign({ userId: 123 }, '[1]');
Drag options to blanks, or click blank then click option'
A12345
BtokenSecret
CmySecretKey
DjwtKey
Attempts:
3 left
💡 Hint
Common Mistakes
Using a number instead of a string as the secret key.
Leaving the secret key empty.
3fill in blank
hard

Fix the error in the code to verify a JWT token using the secret key.

Node.js
const decoded = jwt.verify(token, '[1]');
Drag options to blanks, or click blank then click option'
Ajwt
Btoken
Csecret
DmySecretKey
Attempts:
3 left
💡 Hint
Common Mistakes
Using the token itself as the secret key.
Using a different or undefined secret key.
4fill in blank
hard

Fill both blanks to generate a token with an expiration time of 1 hour.

Node.js
const token = jwt.sign({ id: user.id }, '[1]', { expiresIn: '[2]' });
Drag options to blanks, or click blank then click option'
AmySecretKey
B2h
C1h
D3600
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong expiration format like '2h' or a number without quotes.
Forgetting to add the expiresIn option.
5fill in blank
hard

Fill all three blanks to decode a token safely with error handling.

Node.js
try {
  const decoded = jwt.[1](token, '[2]');
  console.log(decoded.[3]);
} catch (err) {
  console.error('Invalid token');
}
Drag options to blanks, or click blank then click option'
Averify
BmySecretKey
CuserId
Dsign
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sign' instead of 'verify' to decode.
Using a wrong secret key.
Accessing a wrong property from decoded token.