📖 Scenario: You are building a simple Node.js server that handles user login data. Security is very important to protect user information and prevent unauthorized access.
🎯 Goal: Build a basic Node.js server that stores user credentials securely and checks login attempts safely.
📋 What You'll Learn
Create an object to store user credentials
Add a configuration variable for minimum password length
Write a function to validate user login using the stored credentials
Complete the server setup to handle login requests securely
💡 Why This Matters
🌍 Real World
This project shows how to handle user login securely in a Node.js web server, a common task in many web applications.
💼 Career
Understanding basic security practices in Node.js is essential for backend developers to protect user data and build trustworthy applications.
Progress0 / 4 steps
1
Create user credentials object
Create an object called users with these exact entries: "alice": "password123", "bob": "qwerty456", and "carol": "letmein789".
Node.js
Hint
Use a JavaScript object with usernames as keys and passwords as values.
2
Add minimum password length configuration
Add a constant called MIN_PASSWORD_LENGTH and set it to 8.
Node.js
Hint
This helps enforce password security rules.
3
Write login validation function
Write a function called validateLogin that takes username and password as parameters and returns true if the username exists in users and the password matches exactly, otherwise returns false.
Node.js
Hint
Check if the username exists as a key and compare the stored password with the input.
4
Complete server setup with login route
Add code to create an Express server, use express.json() middleware, and add a POST route /login that reads username and password from req.body, uses validateLogin to check credentials, and sends "Login successful" if valid or "Invalid credentials" if not.
Node.js
Hint
Use Express to handle JSON requests and respond based on login validation.
Practice
(1/5)
1. Why is security critical in a Node.js application?
easy
A. To make the app run faster
B. To protect user data and prevent unauthorized access
C. To reduce the size of the code
D. To add more features quickly
Solution
Step 1: Understand the purpose of security
Security is meant to protect sensitive information and prevent unauthorized users from accessing the app.
Step 2: Identify the main goal in Node.js apps
Protecting user data and access is the main goal, not performance or code size.
Final Answer:
To protect user data and prevent unauthorized access -> Option B
Quick Check:
Security = Protect data [OK]
Hint: Security is about protection, not speed or features [OK]
Common Mistakes:
Confusing security with performance optimization
Thinking security adds features
Believing security reduces code size
2. Which of the following is the correct way to hash a password in Node.js using bcrypt?
easy
A. bcrypt.hash(password, saltRounds, callback)
B. bcrypt.encrypt(password, saltRounds)
C. bcrypt.encode(password)
D. bcrypt.secure(password)
Solution
Step 1: Recall bcrypt method names
The correct method to hash passwords is bcrypt.hash with parameters password, saltRounds, and a callback.
Step 2: Check other options for validity
Methods encrypt, encode, and secure do not exist in bcrypt library.
Final Answer:
bcrypt.hash(password, saltRounds, callback) -> Option A
Quick Check:
Use bcrypt.hash() to hash passwords [OK]
Hint: Remember bcrypt.hash for password hashing [OK]
Common Mistakes:
Using non-existent bcrypt methods
Confusing encrypt with hash
Omitting callback in bcrypt.hash
3. What will be the output of this Node.js code snippet?