Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the JWT library.
Express
const jwt = require('[1]');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express' instead of 'jsonwebtoken' for JWT handling.
Forgetting to install the 'jsonwebtoken' package.
✗ Incorrect
The jsonwebtoken package is used to create and verify JWT tokens in Express.
2fill in blank
mediumComplete the code to create a JWT token with a payload.
Express
const token = jwt.sign({ userId: 123 }, '[1]'); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a public or guessable string as the secret.
Passing the payload as the secret key.
✗ Incorrect
The second argument to jwt.sign is the secret key used to sign the token.
3fill in blank
hardFix the error in the code to set token expiration to 1 hour.
Express
const token = jwt.sign({ userId: 123 }, 'mysecretkey', { [1]: '1h' }); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'expire' or 'timeout' instead of 'expiresIn'.
Passing expiration as part of the payload instead of options.
✗ Incorrect
The correct option to set token expiration is expiresIn.
4fill in blank
hardFill both blanks to verify a JWT token and extract the payload.
Express
jwt.[1](token, 'mysecretkey', (err, [2]) => { if (err) { console.error('Invalid token'); } else { console.log([2]); } });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'decode' instead of 'verify' to check token validity.
Naming the decoded payload as 'payload' instead of 'data' (both work but only one is correct here).
✗ Incorrect
Use jwt.verify to check the token and the callback receives the decoded data, often named data.
5fill in blank
hardFill all three blanks to create a JWT token with user info, secret, and expiration.
Express
const token = jwt.sign({ [1]: 'alice' }, '[2]', { [3]: '2h' }); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'user' instead of 'username' as payload key.
Using wrong option name for expiration.
Using a weak or empty secret.
✗ Incorrect
The payload key is 'username', the secret is 'supersecret', and the expiration option is 'expiresIn'.