首先,我对passport.js很陌生,所以也许这最终会成为一个非常天真的问题。我有一个注册策略:

// Configuring Passport
var passport = require('passport');
var expressSession = require('express-session');
var LocalStrategy = require('passport-local').Strategy;
var FacebookStrategy = require('passport-facebook');
app.use(expressSession({secret: 'mySecretKey'}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());

//[...]

  passport.use('signup', new LocalStrategy({
     name : 'name',
     password : 'password',
     email : 'email',
     passReqToCallback : true
   },
   function(req, username, password, done) {
     findOrCreateUser = function(){
       // find a user in Mongo with provided username
       User.findOne({'name':username},function(err, user) {
         // In case of any error return
         if (err){
           console.log('Error in SignUp: '+err);
           return done(err);
         }
        // already exists
      if (user) {
            console.log('User already exists');
            return done(null, false,
            req.flash('message','User Already Exists'));
         } else {
           // if there is no user with that email
           // create the user
           var newUser = new User();
           // set the user's local credentials
           newUser.name = name;
           newUser.password = createHash(password);
           newUser.email = req.param('email');
           /*newUser.firstName = req.param('firstName');
           newUser.lastName = req.param('lastName');*/

           // save the user
           newUser.save(function(err) {
             if (err){
               console.log('Error in Saving user: '+err);
               throw err;
             }
             console.log('User Registration succesful');
             return done(null, newUser);
           });
         }
       });
    };

    // Delay the execution of findOrCreateUser and execute
    // the method in the next tick of the event loop
    process.nextTick(findOrCreateUser);
  })
 );

这就是我如何处理POST上的/register
/* Handle Registration POST */
app.post('/register', passport.authenticate('signup', {
  successRedirect: '/',
  failureRedirect: '/failure_registration',
  failureFlash : true
}));

这总是把我带到failureRedirect链接,而不是成功。输入的数据是正确的,我总是使用不同的用户和邮件来注册。如果这是一个愚蠢的问题,我很抱歉,但我真的不明白为什么它永远不会转到successRedirect
谢谢。
编辑:添加了@robertklep的建议和更正,仍然不起作用。我想指出的是,没有触发错误,也没有打印任何日志。
edit2:序列化/反序列化函数:
passport.serializeUser(function(user, done) {
    done(null, user.id);
});

passport.deserializeUser(function(id, done) {
    User.findById(id, function (err, user) {
        done(err, user);
    });
});

最佳答案

我也有同样的问题,failureredirect总是被执行
首先要诊断的是,我使用了自定义回调方法
http://passportjs.org/docs/authenticate

app.post('/sign_in', function(req, res, next) {
    console.log(req.url);
    passport.authenticate('local-login', function(err, user, info) {
        console.log("authenticate");
        console.log(err);
        console.log(user);
        console.log(info);
    })(req, res, next);
});

然后我就能看出隐藏的错误是什么
authenticate
null
false
{ message: 'Missing credentials' }

对我来说,这很容易诊断,我在请求中发送json而不是表单字段
通过更改修复
app.use(bodyParser.urlencoded({ extended: true }));


app.use(bodyParser.json());

08-15 18:10