Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript User Authentication With Express and Mongo Sessions and Cookies Authenticating the Username and Password

User.authenticate gets error

when i run the code as provided by you i get an error in this line of block

router.post('/login', function(req, res, next) {
if (req.body.email && req.body.password) {
User.authenticate(req.body.email, req.body.password, function (error, user) {
  if (error || !user) {
    var err = new Error('Wrong email or Password.');
    console.log(err);
    err.status = 401;
    return next(err);
  }  else {
    req.session.userId = user._id;
    return res.redirect('/profile');
  }
});

and also with the last line of "return callback();" from compare

    bcrypt.compare("password", "user.password" , function(error, result) {
      console.log(result==true);
      if (result === true) {
        return callback(null, user);
      } else {
        return callback();
      }
    })
  });

what am i missing or doing wrong??

David Leininger
David Leininger
13,034 Points

For the first error, you're missing a closing } on the first "If" inside the post function. You also need to close out the post function itself, as well.

router.post("/login", function(req, res, next) {
  if (req.body.email && req.body.password) {
    User.authenticate(req.body.email, req.body.password, function(error, user) {
      if (error || !user) {
        var err = new Error("Wrong email or Password.");
        console.log(err);
        err.status = 401;
        return next(err);
      } else {
        req.session.userId = user._id;
        return res.redirect("/profile");
      }
    });
  } // closes the if
}); // close the router.post

For the second error, you are missing a ";" as the end of the second to last line to end the bcrypt.compare function. You included the "});" on the last line that closes User.findOne();

    bcrypt.compare("password", "user.password" , function(error, result) {
      console.log(result==true);
      if (result === true) {
        return callback(null, user);
      } else {
        return callback();
      }
    }); // added ;
  // }); this goes with the function that wraps this function