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
Password hashing with bcrypt in Express
📖 Scenario: You are building a simple Express server that needs to securely store user passwords. Instead of saving plain text passwords, you will use bcrypt to hash them before saving.
🎯 Goal: Create an Express app that hashes a user password using bcrypt before saving it.
📋 What You'll Learn
Create a variable with a plain text password string
Add a variable for bcrypt salt rounds
Use bcrypt to hash the password with the salt rounds
Export the hashed password or use it in the app
💡 Why This Matters
🌍 Real World
Password hashing is essential for securely storing user passwords in web applications to protect user data.
💼 Career
Understanding bcrypt and password hashing is a key skill for backend developers working on authentication and security.
Progress0 / 4 steps
1
Create a plain text password variable
Create a variable called plainPassword and set it to the string "mysecret123".
Express
Hint
Use const plainPassword = "mysecret123"; to create the variable.
2
Add bcrypt salt rounds variable
Create a variable called saltRounds and set it to the number 10.
Express
Hint
Use const saltRounds = 10; to set the salt rounds.
3
Hash the password using bcrypt
Import bcrypt at the top with import bcrypt from 'bcrypt';. Then create an async function called hashPassword that uses await bcrypt.hash(plainPassword, saltRounds) to hash the password and returns the hashed password.
Express
Hint
Use async function hashPassword() { const hashed = await bcrypt.hash(plainPassword, saltRounds); return hashed; }
4
Export the hashed password function
Add export default hashPassword; at the end of the file to export the hashPassword function.
Express
Hint
Use export default hashPassword; to export the function.
Practice
(1/5)
1. What is the main purpose of using bcrypt in an Express app?
easy
A. To securely hash user passwords before saving them
B. To speed up server response time
C. To format JSON data
D. To manage user sessions
Solution
Step 1: Understand bcrypt's role
Bcrypt is a library designed to hash passwords securely, making them hard to read if stolen.
Step 2: Identify the correct purpose in Express
In Express apps, bcrypt is used to hash passwords before storing them in a database to protect user data.
Final Answer:
To securely hash user passwords before saving them -> Option A
Quick Check:
Password hashing = Secure storage [OK]
Hint: Bcrypt is for password security, not speed or formatting [OK]
Common Mistakes:
Thinking bcrypt speeds up server
Confusing bcrypt with session management
Using bcrypt for data formatting
2. Which of the following is the correct way to hash a password asynchronously using bcrypt in Express?
easy
A. const hashed = bcrypt.hashSync(password, 10);
B. const hashed = bcrypt.hash(password);
C. const hashed = await bcrypt.hash(password, 10);
Bcrypt's async hash function requires await and two arguments: the password and salt rounds.
Step 2: Check each option
const hashed = await bcrypt.hash(password, 10); uses await bcrypt.hash(password, 10); which is correct async usage. const hashed = bcrypt.hashSync(password, 10); is synchronous, C is wrong function, B misses salt rounds.
Final Answer:
const hashed = await bcrypt.hash(password, 10); -> Option C
Quick Check:
Async hash needs await and salt rounds [OK]
Hint: Async bcrypt hash always uses await and salt rounds [OK]
The code hashes 'secret123' with salt rounds 5, then compares the original password to the hash.
Step 2: Analyze the compare result
Since the password matches the hash, bcrypt.compare returns true, which is logged.
Final Answer:
true -> Option D
Quick Check:
Password matches hash = true [OK]
Hint: Compare returns true if password matches hash [OK]
Common Mistakes:
Expecting false because of low salt rounds
Thinking compare returns the hash
Missing await causing undefined
4. Identify the error in this Express route using bcrypt:
app.post('/signup', async (req, res) => {
const { password } = req.body;
const hashed = bcrypt.hash(password, 10);
// Save hashed password to DB
res.send('User created');
});
medium
A. bcrypt.hash requires 3 arguments, only 2 given
B. Missing await before bcrypt.hash causing a Promise instead of hash
C. bcrypt.hashSync should be used instead of bcrypt.hash
D. Password should not be hashed before saving
Solution
Step 1: Check bcrypt.hash usage
Bcrypt.hash is async and returns a Promise, so it needs await to get the hashed string.
Step 2: Identify missing await effect
Without await, hashed is a Promise, not the actual hash, causing errors when saving.
Final Answer:
Missing await before bcrypt.hash causing a Promise instead of hash -> Option B
Quick Check:
Async bcrypt.hash needs await [OK]
Hint: Always await async bcrypt.hash to get the hash string [OK]
Common Mistakes:
Forgetting await on async bcrypt.hash
Using wrong number of arguments
Thinking hashSync is mandatory
5. You want to create a secure signup route in Express that hashes the password and then verifies it immediately to confirm hashing worked. Which code snippet correctly does this?