User Login: Trial and Error

mtee

Margaret W.N

Posted on August 10, 2020

User Login: Trial and Error

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
      })
})
Enter fullscreen mode Exit fullscreen mode

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')
        }
      });    
Enter fullscreen mode Exit fullscreen mode

I try debug this when I'm well rested.
Day 25

💖 💪 🙅 🚩
mtee
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.

Related

Generating a Json Web Token
100daysofcode Generating a Json Web Token

August 15, 2020

The C in MVC: Controllers
100daysofcode The C in MVC: Controllers

August 12, 2020

User signup & Password Hashing
100daysofcode User signup & Password Hashing

August 8, 2020

User Login: Trial and Error
100daysofcode User Login: Trial and Error

August 10, 2020

Habit tracker API: Updating data
100daysofcode Habit tracker API: Updating data

July 28, 2020