Passport is not redirected after authorization

I have a problem when the page is not redirected when I try to log in using my passport. Return authorization true(correct username, password).

I'm sure it is somewhere inside my function validPassword, but I'm definitely not sure.

Entry route

app.get('/login', function (req, res) {
  res.render('login', {});
});

Login

app.post('/login',
  passport.authenticate('local', { successRedirect: '/',
                                   failureRedirect: '/login' }));

User prototype

User.prototype.validPassword = function(username, unhashedPassword) {
  async.waterfall([
    function (callback) {
      User.find({ "username" : username }, function (err, data) {
        if(err) return handleError(err);
        callback(null, data);
      });
    },
    function (data, callback) {
      var isGood = passwordHash.verify(unhashedPassword, data[0].password);
      callback(null, isGood);
    }
  ], function (err, result) {
    return result;
  });
};

Local strategy

passport.use(new LocalStrategy(
  function(username, password, done) {
    var unhashedPassword = password;
    var passedUsername = username;
    User.findOne({ username: username }, function(err, user) {
      if (err) { return done(err); }
      if (!user) {
        return done(null, false, { message: 'Incorrect username.' });
      }
      if (!user.validPassword(passedUsername, unhashedPassword)) {
        return done(null, false, { message: 'Incorrect password.' });
      }
      return done(null, user);
    });
  }
));

No errors are printed on my console, so I'm a bit puzzled. Is it possible to return isGoodin the wrong format? Any help would be great.

+4
source share
1 answer

. validPassword.

console.log, , . console.log prototype passwordHash.verify(unhashedPassword, this.password).

, :

LOG: valid username (Strategy)
LOG: invalid password (Strategy)
LOG: true (Prototype)

, LOG: true (prototype) LOG: invalid password (Strategy), , , - .

, , 15 , .

auth.

User.prototype.validPassword

User.prototype.validPassword = function(unhashedPassword) {
  return passwordHash.verify(unhashedPassword, this.password);
};

LocalStrategy

passport.use(new LocalStrategy({
    usernameField: 'username',
    passwordField: 'password'
  },
  function(username, password, done) {
    var unhashedPassword = password;
    var passedUsername = username;
    process.nextTick(function () {
      User.findOne({ username: passedUsername }, function(err, user) {
        console.log('within local strategy', user);
        if (err) { 
          console.log('Error:', err);
          return done(err); 
        }
        if (!user) {
          console.log('Incorrect username:');
          return done(null, false, { message: 'Incorrect username.' });
        }
        if (!user.validPassword(unhashedPassword)) {
          return done(null, false, { message: 'Incorrect password.' });
        }
        return done(null, user);
      });
    });
  }
));

app.post('/login',
  passport.authenticate('local', { failureRedirect: '/login' }),
  function(req, res) {
    res.redirect('/');
  });
+3

Source: https://habr.com/ru/post/1538211/


All Articles