Last active
March 9, 2020 19:44
-
-
Save awmpietro/753d5fb27f2efea18df0c717461428be to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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