User Login: Trial and Error
Margaret W.N
Posted on August 10, 2020
I added a route to handle login. This route makes a post request and compares the provided password with the saved password. Then returns a success message.
The first step is to find the user. Since the email is unique I'll retrieve the user by searching for the email using findOne.
router.route('/users/login')
.post(async (req, res) => {
User.findOne({ email: req.body.email }, (err, user) => {
if (err) {
return res.send(err);
}
return user
})
})
Next i tried comparing both passwords using bcrypt compare but i keep getting an error. My guess is that it's a syntax error that I'm yet to identify. Here is the code though.
await bcyrpt.compare(req.body.password, user.password, (err, res) => {
if(err) {
res.send(err)
}
if (req.body.password != user.password) {
res.json({ success: false, message: 'passwords do not match' });
} else {
res.send('Log in Sucessfull')
}
});
I try debug this when I'm well rested.
Day 25
💖 💪 🙅 🚩
Margaret W.N
Posted on August 10, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.