Sometimes, we want to hash a password using bcrypt inside a JavaScript async function.
In this article, we’ll look at how to hash a password using bcrypt inside a JavaScript async function.
How to hash a password using bcrypt inside a JavaScript async function?
To hash a password using bcrypt inside a JavaScript async function, we can use the bcrypt.hash
method.
For instance, we write:
const bcrypt = require('bcrypt');
(async () => {
const password = 'password'
const saltRounds = 2
const hashedPassword = await bcrypt.hash(password, saltRounds)
console.log(hashedPassword)
})()
We call bcrypt.hash
with the password
string that we want to hash and the number of saltRounds
.
It returns a promise so we can use await
to get the resolved value, which is the hashed password
.
Conclusion
To hash a password using bcrypt inside a JavaScript async function, we can use the bcrypt.hash
method.