E
E
Emptyform2016-10-12 10:55:22
Node.js
Emptyform, 2016-10-12 10:55:22

PassportJS :: How to get the error text given in the strategy when calling isAuthenticate()?

Hey!
Using the local strategy passport.js

module.exports = new LocalStrategy(
  {
    usernameField: 'login',
    passwordField: 'password'
  },
  (login, password, done) => {
    User.findOne({ login: login }, (err, user) => {
      if (err) return done(err);

      if (!user || !user.checkPassword(password)) {
        return done(null, false, { message: 'Incorrect username or password' });
      };

      return done(null, user);
    });
  }
);

How do I get the error text from message on a subsequent call to this.isAuthenticated() ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Emptyform, 2016-10-12
@Emptyform

In short here:

exports.post = function*(next) {
  var self = this;

  yield passport.authenticate('local', function* (err, user, info) {
    if (err) throw err;

    if (user === false) {
      self.status = 401;
      self.body = info.message;
    } else {
      yield self.login(user);
    }

    self.redirect('/');
  });
};

In the question, of course, this.isAuthenticated() is written, but the point was how to show the client a message if something went wrong.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question