带护照的快闪信息不起作用

Eri*_*ron 4 node.js express passport.js

按照示例https://scotch.io/tutorials/easy-node-authentication-setup-and-local我创建了一个简单的注册应用程序来测试护照。

我正在使用 Express 4 和连接闪存模块。

我的路线如下,即在GET /signup我显示带有任何可能的signupMessage闪存消息(始终未定义)的注册表单上。在POST /signup我尝试验证凭据时:

router.get('/signup', function(req, res) {
  debug('GET signup flash message "%s"',req.flash('signupMessage'));
  res.render('signup',  { message: req.flash('signupMessage') });
});

// process the signup form
router.post('/signup', passport.authenticate('local-signup', {
  successRedirect : '/profile', // redirect to the secure profile section
  failureRedirect : '/signup', // redirect back to the signup page if there is an error
  failureFlash : true // allow flash messages
}));
Run Code Online (Sandbox Code Playgroud)

接下来是我的护照相关代码。它有效,但return done(null, false, req.flash('signupMessage', 'That email is already taken.'));似乎必须设置请求 flash 消息之类的行似乎不起作用,因为 flash 消息从未显示在 GET 方法显示的注册表单中。

passport.use('local-signup', new LocalStrategy({
    // by default, local strategy uses username and password, we will override with email
    usernameField : 'email',
    passwordField : 'password',
    passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, email, password, done) {

    debug("Auth");

    // asynchronous
    // User.findOne wont fire unless data is sent back
    process.nextTick(function() {

    debug("before findOne");

      // find a user whose email is the same as the forms email
      // we are checking to see if the user trying to login already exists
      User.findOne({ 'local.email' :  email }, function(err, user) {

        debug("inside findOne");

          // if there are any errors, return the error
          if (err) {
            return done(err);
          }

          // check to see if theres already a user with that email
          if (user) {
            debug('user exists');
            return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
          } else {

            debug('creating new user');

            // if there is no user with that email
            // create the user
            var newUser            = new User();

            // set the user's local credentials
            newUser.local.email    = email;
            newUser.local.password = newUser.generateHash(password);

            // save the user
            newUser.save(function(err) {
              if (err) {
                throw err;
              }
              return done(null, newUser);
            });
          }

      });    

    });

}));
Run Code Online (Sandbox Code Playgroud)

在寻求帮助后,我也尝试输入:

return done(null, false, {message: 'That email is already taken.'});
Run Code Online (Sandbox Code Playgroud)

并在GET /signup方法中使用:

router.get('/signup', function(req, res) {
  res.render('signup',  { message: req.flash('error') });
});
Run Code Online (Sandbox Code Playgroud)

但这不起作用。

任何人都可以帮助我吗?

谢谢。

Eri*_*ron 6

对于任何可能遇到类似问题的人,我发现我的错误在于会话配置。

如文档所示,如果您设置了安全 cookie 选项,则您的连接必须通过 HTTPS 进行,否则将不会创建 cookie。这就是正常消息和闪现消息都不起作用的原因。