Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 11, 2020 07:41 pm GMT

Password authentication

A user will typically input the email/username and password to log into a system. How do we verify the inputs and successfully retrieve their account?

I'm using mongoose findOne() inbuilt method, and passing in a condition to retrieve the User email similar to the email input from the request. I'm also passing an asynchronous error first callback function. If an error occurs during retrieval the function exists immediately. If the user is successfully retrieved the saved password and password input are compared using bcrypt.compare( ).

router.route('/users/login')    .post((req, res) => {      User.findOne({ email: req.body.email }, async (err, user) => {        if (err) {          return res.send(err);        }        if (await bcrypt.compare(req.body.password, user.password)) {          return res.send(`Welcome back ${user.firstName}`)        }        return res.send('Wrong Password');      })    });

bcrypt.compare( ) resolves to either true or false. If it results to true a welcome back message with the users first name will be sent back. If false we'll return wrong Password.

Lets Create a User
Alt Text

Output using the correct Password
Alt Text

Output using the wrong Password
Alt Text

Nothing beats a working solution!
Day 26


Original Link: https://dev.to/mtee/password-authentication-3i24

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To