Challenge - 5 Problems
Authentication Flow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the response status code when a user provides valid credentials?
Consider an Express app with a POST /login route that checks username and password. If credentials are correct, it responds with status 200 and a JSON message {"message": "Login successful"}. What status code will the server respond with when valid credentials are sent?
Express
app.post('/login', (req, res) => { const { username, password } = req.body; if(username === 'user' && password === 'pass') { res.status(200).json({ message: 'Login successful' }); } else { res.status(401).json({ message: 'Unauthorized' }); } });
Attempts:
2 left
💡 Hint
Think about the HTTP status code for a successful login.
✗ Incorrect
A status code of 200 means the request was successful. Since the credentials are valid, the server responds with 200.
❓ state_output
intermediate2:00remaining
What is the value of req.user after successful authentication?
In an Express middleware that authenticates a user and attaches user info to req.user, what will req.user contain after successful login?
Express
function authMiddleware(req, res, next) {
const token = req.headers['authorization'];
if(token === 'valid-token') {
req.user = { id: 1, name: 'Alice' };
next();
} else {
res.status(401).send('Unauthorized');
}
}Attempts:
2 left
💡 Hint
Look at what the middleware assigns to req.user when the token is valid.
✗ Incorrect
The middleware sets req.user to an object with id and name when the token is valid.
📝 Syntax
advanced2:00remaining
Which option causes a syntax error in Express route definition?
Identify the option that will cause a syntax error when defining an Express route.
Express
app.get('/profile', (req, res) => { res.send('User profile'); });
Attempts:
2 left
💡 Hint
Check for missing commas or parentheses.
✗ Incorrect
Option A is missing a comma between the path and the callback function, causing a syntax error.
🔧 Debug
advanced2:00remaining
Why does this authentication middleware always call next() even with invalid tokens?
Given the middleware below, why does it always call next() even when the token is not 'valid-token'?
Express
function authMiddleware(req, res, next) {
const token = req.headers['authorization'];
if(token = 'valid-token') {
next();
} else {
res.status(401).send('Unauthorized');
}
}Attempts:
2 left
💡 Hint
Look carefully at the if condition syntax.
✗ Incorrect
The if condition uses '=' which assigns 'valid-token' to token instead of comparing it. This always evaluates to true, causing it to always call next(), even for invalid tokens.
🧠 Conceptual
expert2:00remaining
What is the main purpose of using sessions in authentication flows?
Why do Express apps often use sessions when handling authentication?
Attempts:
2 left
💡 Hint
Think about how servers remember who you are after you log in.
✗ Incorrect
Sessions allow the server to remember a user's login state across multiple requests, so the user doesn't have to log in every time.