0
0
Expressframework~30 mins

User login flow in Express - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use res.json() to send the response and app.listen(3000) to start the server.