0
0
Expressframework~10 mins

JWT token creation in Express - 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.

Express
const jwt = require('[1]');
Drag options to blanks, or click blank then click option'
Ajsonwebtoken
Bexpress
Cbody-parser
Dcors
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express' instead of 'jsonwebtoken' for JWT handling.
Forgetting to install the 'jsonwebtoken' package.
2fill in blank
medium

Complete 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'
Aexpress
Bmysecretkey
C12345
Dtoken
Attempts:
3 left
💡 Hint
Common Mistakes
Using a public or guessable string as the secret.
Passing the payload as the secret key.
3fill in blank
hard

Fix 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'
Atimeout
Bexpire
Cduration
DexpiresIn
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'expire' or 'timeout' instead of 'expiresIn'.
Passing expiration as part of the payload instead of options.
4fill in blank
hard

Fill 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'
Averify
Bdecode
Cpayload
Ddata
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).
5fill in blank
hard

Fill 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'
Ausername
Bsupersecret
CexpiresIn
Duser
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.