Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
User login flow
📖 Scenario: You are building a simple user login flow for a website using Express.js. Users will send their username and password to the server, and the server will check if the credentials are correct.
🎯 Goal: Create an Express.js server that accepts login requests, checks user credentials from a predefined list, and responds with success or failure messages.
📋 What You'll Learn
Create a users object with exact username-password pairs
Add a variable to hold the login success message
Write a POST route handler for '/login' that checks credentials
Send the correct response message based on login success or failure
💡 Why This Matters
🌍 Real World
User login flows are essential for websites and apps to authenticate users securely.
💼 Career
Understanding how to build login routes with Express is a key skill for backend web developers.
Progress0 / 4 steps
1
DATA SETUP: Create users data
Create a constant object called users with these exact entries: 'alice': 'wonderland', 'bob': 'builder', 'charlie': 'chocolate'.
Express
Hint
Use a JavaScript object with usernames as keys and passwords as values.
2
CONFIGURATION: Add success message variable
Add a constant string variable called successMessage and set it to 'Login successful!'.
Express
Hint
Use const to create a string variable for the success message.
3
CORE LOGIC: Create login POST route
Write an Express POST route handler for '/login' that extracts username and password from req.body, checks if users[username] equals password, and stores the result in a boolean variable called isValidUser.
Express
Hint
Use app.post with a callback that destructures username and password from req.body.
4
COMPLETION: Send response based on login result
Inside the /login route handler, send a JSON response with { message: successMessage } if isValidUser is true, otherwise send { message: 'Invalid username or password' }. Then, start the Express server listening on port 3000.
Express
Hint
Use res.json() to send the response and app.listen(3000) to start the server.
Practice
(1/5)
1. What is the main purpose of a user login flow in an Express app?
easy
A. To verify the user's identity before granting access
B. To display the homepage content
C. To log server errors
D. To serve static files like images
Solution
Step 1: Understand the login flow goal
The login flow is designed to check who the user is by verifying credentials.
Step 2: Identify the correct purpose
Granting access only after verification matches the login flow's main purpose.
Final Answer:
To verify the user's identity before granting access -> Option A
Quick Check:
Login flow = Verify user identity [OK]
Hint: Login flow means checking user identity first [OK]
Common Mistakes:
Confusing login flow with serving static files
Thinking login flow logs errors
Assuming login flow shows homepage content
2. Which Express route method is best suited to securely receive login form data?
easy
A. app.put('/login', ...)
B. app.get('/login', ...)
C. app.post('/login', ...)
D. app.delete('/login', ...)
Solution
Step 1: Recall HTTP methods for form data
POST is used to send data securely from forms, unlike GET which appends data in URL.
Step 2: Match method to login data handling
Login forms should use POST to keep credentials hidden and secure.
Final Answer:
app.post('/login', ...) -> Option C
Quick Check:
Use POST for login data [OK]
Hint: Use POST to send login data securely [OK]
Common Mistakes:
Using GET exposes credentials in URL
PUT and DELETE are not for login forms
Confusing route methods for form submission
3. What will be the output if the following Express code is used for login and the user provides correct credentials?
D. Assigning session to 'res.session' instead of 'req.session'
Solution
Step 1: Check session assignment
Session data should be stored on req.session, not res.session.
Step 2: Confirm correct session usage
Using res.session will cause undefined error; req.session is correct.
Final Answer:
Assigning session to 'res.session' instead of 'req.session' -> Option D
Quick Check:
Session stored on req, not res [OK]
Hint: Session is on req, not res object [OK]
Common Mistakes:
Confusing req and res objects
Ignoring missing status code on failure
Thinking '==' causes error here
5. You want to keep users logged in across pages after login in Express. Which approach correctly implements this using sessions?
1. Use express-session middleware
2. On successful login, save username in req.session
3. On other routes, check if req.session.user exists
4. If exists, allow access; else redirect to login
hard
A. Sessions should not be used; use cookies only
B. This approach is correct and follows best practices
C. Store user info in res.locals instead of session
D. Use GET method to store session data
Solution
Step 1: Understand session usage in Express
express-session middleware manages sessions; storing user info in req.session keeps login state.
Step 2: Verify access control logic
Checking req.session.user on other routes to allow or redirect is standard practice.
Final Answer:
This approach is correct and follows best practices -> Option B