Challenge - 5 Problems
JWT Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this JWT token creation code?
Consider this Express route that creates a JWT token. What will be the response body when a POST request is made with {"userId": "123"}?
Express
import express from 'express'; import jwt from 'jsonwebtoken'; const app = express(); app.use(express.json()); app.post('/login', (req, res) => { const { userId } = req.body; const token = jwt.sign({ id: userId }, 'secretKey', { expiresIn: '1h' }); res.json({ token }); });
Attempts:
2 left
💡 Hint
The jwt.sign method returns a string token when called correctly.
✗ Incorrect
The code creates a JWT token with the userId in the payload and returns it as JSON. No errors occur if userId is provided.
📝 Syntax
intermediate1:30remaining
Which option correctly creates a JWT token with payload {id: 5} and secret 'key'?
Choose the code snippet that correctly creates a JWT token using jsonwebtoken in Express.
Attempts:
2 left
💡 Hint
The payload must be an object, and the secret a string.
✗ Incorrect
Option B correctly calls jwt.sign with payload object and secret string. Option B has wrong syntax. Option B uses undefined variable key. Option B has wrong options syntax.
🔧 Debug
advanced2:00remaining
What error does this JWT token creation code raise?
Analyze the code below. What error will occur when this Express route is called?
Express
app.post('/token', (req, res) => {
const token = jwt.sign({ user: req.body.user });
res.send(token);
});Attempts:
2 left
💡 Hint
jwt.sign requires a secret string as second argument.
✗ Incorrect
The code calls jwt.sign without the secret key argument, causing a TypeError.
❓ state_output
advanced2:00remaining
What is the value of 'decoded' after verifying this JWT token?
Given this code snippet, what will be the value of 'decoded' if the token is valid?
Express
const token = jwt.sign({ id: 10, role: 'admin' }, 'mySecret');
const decoded = jwt.verify(token, 'mySecret');Attempts:
2 left
💡 Hint
jwt.verify returns the payload plus issued at timestamp.
✗ Incorrect
The decoded object includes the original payload plus an 'iat' field with the issued timestamp.
🧠 Conceptual
expert2:30remaining
Which option best explains why JWT tokens include a secret key during creation?
Why do we provide a secret key when creating a JWT token with jwt.sign?
Attempts:
2 left
💡 Hint
Think about what signing a token means.
✗ Incorrect
The secret key is used to sign the token, allowing verification that it was issued by the server and not altered.