route.post无法运行passport.use

时间:2015-09-21 04:48:40

标签: node.js express passport.js passport-local

我被提出了一个302错误,我正在努力调试提交,我假设我错过了我的护照。用于身份验证。谁能看到我出错的地方?

user.js的

passport.use(new LocalStrategy(
    function(username, password, done){
        User.getUserByUsername(username, function(err, user){
            if(err) throw err;
            if(!user){
                console.log('Unknown User');
                return done(null, false,{message: 'Unknown User'});
            }

      User.comparePassword(password, user.password, function(err, isMatch){
        if (err) throw err;
        if(isMatch){
          return done(null, user);
        } else {
          console.log('Invalid password');
          return done(null, false, {message: 'Invalid password'});
        }
      });
    });
  }
));

router.post('/login', passport.authenticate('local', {
    failureRedirect: '/users/login',
    failureFlash: 'Invalid Username or Password',
  }),
  // If user authenticates
  function(req, res) {
    console.log('Authentication successful');
    req.flash('success', 'You are logged in');
    res.redirect('/');
  });

模型

module.exports.comparePassword = function(candidatePassword, hash, callback){
  bcrypt.compare(candidatePassword, hash, function(err, isMatch){
    if(err) return callback(err);
    callback(null, isMatch);
  });
};

module.exports.getUserById = function(id, callback){
    User.findById(id, callback);
};

module.exports.getUserByUsername = function(username, callback){
    var query = {username: username};
    User.findOne(query, callback);
};

extends layout

block content
    h1 Login
    p Please login below
    form(method='post',action='/users/login',enctype='multipart/form-data')
        .form-group
            label Username
            input.form-control(name='username', type='text')
        .form-group
            label Password
            input.form-control(name='password', type='password')
        input.btn.btn-default(name='submit', type='submit',value='Login')

据我所知,一切都应该正常开火。或者任何人都可以指出我正确的方向来调试router.post?

1 个答案:

答案 0 :(得分:0)

事实证明问题不在于Express或Passport。 在进一步调试之后,我从表单中追踪了'undefined'变量,以发现multipart/form-data' enctype是应该责备的。