I am try to encrypt password in Node js by use of const bcrypt = require(‘bcrypt’); but its not hashing. When you try on terminal its hashing but not on database, just posting a plain text that was typed on an input.
Installation and imports
const bcrypt = require(‘bcrypt’);
npm install bcrypt@latest
// Sign-up route
app.post('/signup', async (req, res) => {
const { fullName, email, password } = req.body;
try {
// Hash the password before storing it in the database
const hashedPassword = await bcrypt.hash(password, 10);
// Insert user data into the database
const newUser = { fullName, email, password: hashedPassword };
db.query('INSERT INTO users SET ?', newUser, (err) => {
if (err) {
console.error('Error inserting user:', err);
res.status(500).json({ error: 'Internal server error' });
return;
}
res.json({ message: 'User registered successfully' });
});
} catch (error) {
console.error('Error hashing password:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
I am expecting password to hashed, for example password: 123 , this $2b$10$8hL4zs.XUIXpc6sxf/kfYeKWAcwduZaMlUsZoRz8.cRc5kz9EzEV2
should be posted to database datatype password
3
Answers
Thanks everyone who contributed, I have solved the issue, I clear node js cache and it worked
Hope this helps you.
bcrypt.hashSync(plainPassword, 10);
you can use instead of bcrypt.hash but be carefull because
hash, asynchronously function
hashSync, synchronous Function