Challenge - 5 Problems
Express Login Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Express login route?
Consider this Express route handling a POST login request. What will the server respond with if the user provides correct credentials?
Express
app.post('/login', (req, res) => { const { username, password } = req.body; if (username === 'admin' && password === '1234') { res.status(200).send('Login successful'); } else { res.status(401).send('Unauthorized'); } });
Attempts:
2 left
💡 Hint
Check the condition inside the if statement comparing username and password.
✗ Incorrect
The route checks if username is 'admin' and password is '1234'. If true, it sends status 200 with 'Login successful'. Otherwise, it sends status 401 with 'Unauthorized'.
❓ state_output
intermediate2:00remaining
What is the value of 'isLoggedIn' after this middleware runs?
Given this Express middleware that sets a flag on the request object, what will be the value of 'req.isLoggedIn' after it runs if the user is authenticated?
Express
function authMiddleware(req, res, next) {
if (req.headers.authorization === 'Bearer validtoken') {
req.isLoggedIn = true;
} else {
req.isLoggedIn = false;
}
next();
}Attempts:
2 left
💡 Hint
Look at how 'req.isLoggedIn' is assigned based on the authorization header.
✗ Incorrect
The middleware checks if the authorization header matches 'Bearer validtoken'. If yes, it sets 'req.isLoggedIn' to true; otherwise false.
🔧 Debug
advanced3:00remaining
Why does this login route always return 200 'Login successful'?
Examine the following Express login route. Why does it always respond with 200 'Login successful' even when incorrect credentials are sent?
Express
app.post('/login', (req, res) => { const { username, password } = req.body; if (username === 'admin' && password === '1234') { res.status(200).send('Login successful'); } else { res.status(401).send('Unauthorized'); } });
Attempts:
2 left
💡 Hint
Check the operators used in the if condition.
✗ Incorrect
The code uses '=' which assigns values instead of comparing. This causes the condition to always be truthy but the logic is broken, leading to unexpected behavior.
📝 Syntax
advanced3:00remaining
Which option correctly parses JSON body in Express login route?
You want to access 'req.body' in your Express login route. Which code snippet correctly enables JSON body parsing?
Attempts:
2 left
💡 Hint
Look for middleware that parses JSON bodies globally.
✗ Incorrect
Using 'app.use(express.json())' enables JSON body parsing for all routes. Other options either parse URL-encoded data or use middleware incorrectly.
🧠 Conceptual
expert3:00remaining
What is the main security risk if you do not hash passwords in a login flow?
In a user login flow, passwords are stored in plain text in the database. What is the biggest risk of this practice?
Attempts:
2 left
💡 Hint
Think about what happens if someone steals the database data.
✗ Incorrect
Storing passwords in plain text means anyone who accesses the database can see all passwords. This is a major security risk.