What if your users' passwords were stolen because you didn't hash them right?
Why Password hashing with bcrypt in Express? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine storing user passwords as plain text in your database. If someone hacks your system, they get all passwords instantly.
Or you try to hash passwords yourself with simple methods, but it's easy to make mistakes that leave accounts unsafe.
Manual password handling is risky and slow. Simple hashes can be cracked quickly by attackers using common tools.
Writing your own secure hashing is tricky and easy to get wrong, leading to data breaches and lost trust.
bcrypt automatically hashes passwords with strong, slow algorithms and adds a unique salt to each one.
This makes stored passwords very hard to crack, even if the database is stolen.
const hashed = crypto.createHash('sha256').update(password).digest('hex');
const hashed = await bcrypt.hash(password, 10);Securely storing passwords so users' accounts stay safe even if your database is compromised.
A website uses bcrypt to hash user passwords before saving. When users log in, bcrypt checks the password safely without exposing the original.
Storing plain passwords is dangerous and easy to exploit.
Manual hashing is error-prone and often insecure.
bcrypt provides a reliable, secure way to hash passwords with salt and work factor.
Practice
bcrypt in an Express app?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 AQuick Check:
Password hashing = Secure storage [OK]
- Thinking bcrypt speeds up server
- Confusing bcrypt with session management
- Using bcrypt for data formatting
Solution
Step 1: Identify asynchronous bcrypt hashing syntax
Bcrypt's async hash function requiresawaitand two arguments: the password and salt rounds.Step 2: Check each option
const hashed = await bcrypt.hash(password, 10); usesawait 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 CQuick Check:
Async hash needs await and salt rounds [OK]
- Using synchronous hashSync instead of async
- Calling compare instead of hash
- Omitting salt rounds argument
const bcrypt = require('bcrypt');
async function test() {
const password = 'secret123';
const hash = await bcrypt.hash(password, 5);
const match = await bcrypt.compare('secret123', hash);
console.log(match);
}
test();Solution
Step 1: Understand bcrypt.hash and bcrypt.compare
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.comparereturns true, which is logged.Final Answer:
true -> Option DQuick Check:
Password matches hash = true [OK]
- Expecting false because of low salt rounds
- Thinking compare returns the hash
- Missing await causing undefined
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');
});Solution
Step 1: Check bcrypt.hash usage
Bcrypt.hash is async and returns a Promise, so it needsawaitto get the hashed string.Step 2: Identify missing await effect
Withoutawait,hashedis a Promise, not the actual hash, causing errors when saving.Final Answer:
Missing await before bcrypt.hash causing a Promise instead of hash -> Option BQuick Check:
Async bcrypt.hash needs await [OK]
- Forgetting await on async bcrypt.hash
- Using wrong number of arguments
- Thinking hashSync is mandatory
Solution
Step 1: Check async usage and salt rounds
app.post('/signup', async (req, res) => { const { password } = req.body; const hash = await bcrypt.hash(password, 12); const valid = await bcrypt.compare(password, hash); if (valid) res.send('Signup successful'); else res.status(500).send('Hashing error'); }); uses async/await correctly and provides salt rounds (12) to bcrypt.hash, which is best practice.Step 2: Verify immediate password check
It compares the original password with the hash using await bcrypt.compare, then sends success if valid.Step 3: Analyze other options
app.post('/signup', (req, res) => { const { password } = req.body; const hash = bcrypt.hashSync(password, 12); const valid = bcrypt.compareSync(password, hash); if (valid) res.send('Signup successful'); else res.status(500).send('Hashing error'); }); uses sync methods which block the server, C misses await causing Promises, D misses salt rounds in hash.Final Answer:
Option A code snippet with async/await and salt rounds -> Option AQuick Check:
Async hash with salt rounds + compare = correct [OK]
- Using synchronous bcrypt methods in async routes
- Forgetting await causing Promises
- Omitting salt rounds in hash
