Skip to content

Instantly share code, notes, and snippets.

@awmpietro
Last active March 9, 2020 19:44
Show Gist options
  • Select an option

  • Save awmpietro/753d5fb27f2efea18df0c717461428be to your computer and use it in GitHub Desktop.

Select an option

Save awmpietro/753d5fb27f2efea18df0c717461428be to your computer and use it in GitHub Desktop.
const bcrypt = require("bcrypt");
// Generate a new hashed password from a given string
const generateHashedPassword = async plainPassword => {
return new Promise( async (resolve, reject) => {
try{
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(plainPassword, salt);
resolve(hashedPassword);
} catch(error) {
reject(error)
}
})
}
//Compare a previouly hashed password with a plain text password
const comparePasswords = async(hashedPassword, plainPassword) => {
return new Promise( (resolve, reject) => {
try {
const isMatch = await bcrypt.compare(plainPassword, hashedPassword);
if(isMatch){
resolve(isMatch)
} else {
reject(new Error("Invalid password."))
}
} catch(error) {
reject(error)
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment