Challenge - 5 Problems
Refresh Token Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
What is the main purpose of a refresh token in an Express authentication flow?
Choose the best explanation for why refresh tokens are used alongside access tokens.
Attempts:
2 left
💡 Hint
Think about how users stay logged in without entering their password repeatedly.
✗ Incorrect
Refresh tokens let the client ask the server for a new access token when the old one expires, so users don't have to log in again.
❓ component_behavior
intermediate2:00remaining
What happens when an expired access token is used with a valid refresh token in Express?
Given a client sends an expired access token and a valid refresh token to the server, what is the expected server behavior?
Attempts:
2 left
💡 Hint
Refresh tokens are meant to help when access tokens expire.
✗ Incorrect
The server uses the refresh token to create a new access token, so the user stays logged in without interruption.
📝 Syntax
advanced3:00remaining
Which Express middleware snippet correctly verifies a refresh token from cookies?
Select the code snippet that properly extracts and verifies a refresh token stored in cookies using jsonwebtoken.
Express
const jwt = require('jsonwebtoken');
function verifyRefreshToken(req, res, next) {
// Your code here
}Attempts:
2 left
💡 Hint
Refresh tokens are often stored in cookies and verified with the refresh secret.
✗ Incorrect
Option A correctly reads the refresh token from cookies and verifies it with the refresh secret. It handles errors and calls next() properly.
❓ state_output
advanced2:00remaining
What is the output of this Express route when the refresh token is missing?
Consider this Express route snippet handling token refresh. What response does the client get if no refresh token cookie is sent?
Express
app.post('/refresh', (req, res) => { const refreshToken = req.cookies.refreshToken; if (!refreshToken) { return res.status(401).json({ message: 'No refresh token provided' }); } // further logic omitted });
Attempts:
2 left
💡 Hint
Check the condition when refreshToken is falsy.
✗ Incorrect
The route returns a 401 Unauthorized status with a message if the refresh token is missing.
🔧 Debug
expert3:00remaining
Why does this Express refresh token route cause a runtime error?
Identify the cause of the runtime error in this refresh token route code snippet.
Express
app.post('/token', (req, res) => { const refreshToken = req.cookies.refreshToken; jwt.verify(refreshToken, process.env.REFRESH_SECRET, (err, user) => { if (err) return res.sendStatus(403); const accessToken = jwt.sign({ name: user.name }, process.env.ACCESS_SECRET, { expiresIn: '15m' }); }); res.json({ accessToken }); });
Attempts:
2 left
💡 Hint
Check where accessToken is declared and used.
✗ Incorrect
accessToken is declared inside the callback but used outside, so it is undefined when res.json runs, causing a ReferenceError.