Challenge - 5 Problems
Session Auth Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens after a successful login with express-session?
Consider this Express route using express-session for login:
What will be stored in the session after a successful login?
app.post('/login', (req, res) => {
const { username, password } = req.body;
if (username === 'user' && password === 'pass') {
req.session.user = username;
res.send('Logged in');
} else {
res.status(401).send('Unauthorized');
}
});What will be stored in the session after a successful login?
Attempts:
2 left
💡 Hint
Look at what property of req.session is assigned after login.
✗ Incorrect
The code assigns req.session.user = username, so the session stores the username string 'user'. Password is not saved.
❓ state_output
intermediate2:00remaining
What is the session state after logout?
Given this logout route:
What is the state of req.session after logout completes successfully?
app.post('/logout', (req, res) => {
req.session.destroy(err => {
if (err) {
return res.status(500).send('Error');
}
res.send('Logged out');
});
});What is the state of req.session after logout completes successfully?
Attempts:
2 left
💡 Hint
What does req.session.destroy() do to the session?
✗ Incorrect
Calling req.session.destroy() removes the session from store and clears req.session, making it undefined or null.
📝 Syntax
advanced2:00remaining
Which code correctly configures express-session middleware?
Choose the correct way to set up express-session middleware with a secret and resave option:
Attempts:
2 left
💡 Hint
Check the syntax for calling middleware and option types.
✗ Incorrect
Option A correctly calls session() with an options object. A uses assignment instead of function call. C uses string 'false' instead of boolean false. D uses undefined variable saveUninitialized.
🔧 Debug
advanced2:00remaining
Why does session data not persist between requests?
A developer uses express-session but notices session data resets on every request. Which mistake causes this?
app.use(session({
secret: 'secret',
resave: false,
saveUninitialized: false
}));
app.get('/set', (req, res) => {
req.session.value = 42;
res.send('Value set');
});
app.get('/get', (req, res) => {
res.send('Value: ' + req.session.value);
});Attempts:
2 left
💡 Hint
Think about how sessions track users across requests.
✗ Incorrect
If the client blocks cookies, the session ID cookie is not sent back, so server treats each request as new session.
🧠 Conceptual
expert2:00remaining
What is the main security risk if express-session secret is exposed?
If the secret used in express-session middleware is leaked, what is the biggest risk?
Attempts:
2 left
💡 Hint
Think about what the secret is used for in cookie signing.
✗ Incorrect
The secret signs the session ID cookie. If exposed, attackers can create fake cookies to impersonate users.