H
H
hollanditkzn2017-10-27 14:59:50
Node.js
hollanditkzn, 2017-10-27 14:59:50

How to display an error in passport-local-mongoose with an incorrect password and not a 401 page output?

I have such a problem that when I enter the wrong password, a page with the inscription Unauthorized is displayed. And you need to render the same page with an error, then that the username or password is not correct. I tried a little, but so I did not catch up with where to do what.

router.post('/', passport.authenticate('local'), (req, res) => {
if(status === 401) {
res.render('index', {message: 'Неверно логин или пароль'})
} else {
  res.redirect('/users');
}
});

I'm just a beginner in node js, I don't know how to display if the status is 401, then another action will be
Link to the passport-local-mongoose
module I also tried this
router.post('/', passport.authenticate('local'), (req, res, err) => {
  if(err){
    res.render('index', {message: 'Неправильный логин или пароль'});
  } else {
    res.redirect('/users');
  }
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2017-10-27
@hollanditkzn

Everything is written in the documentation for the module.

var User = mongoose.model('Users', UserSchema);

User.register({username:'username', active: false}, 'password', function(err, user) {
  if (err) { ... }

  var authenticate = User.authenticate();
  authenticate('username', 'password', function(err, result) {
    if (err) { ... }
    
  });
});

Here you process registration and authorization, if authorization error, then do something.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question