An Interest In:
Web News this Week
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
- March 15, 2024
- March 14, 2024
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.
Output using the correct Password
Output using the wrong Password
Nothing beats a working solution!
Day 26
Original Link: https://dev.to/mtee/password-authentication-3i24
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To