【问题标题】:Express flash messages with passport doesn't work带护照的快速闪信不起作用
【发布时间】:2015-06-01 08:52:03
【问题描述】:

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

我正在使用 Express 4 和 connect-flash 模块。

我的路线如下,即在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
}));

接下来是我的护照相关代码。它可以工作,但像 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);
            });
          }

      });    

    });

}));

在寻求帮助后我也尝试提出:

return done(null, false, {message: 'That email is already taken.'});

并在GET /signup 方法中使用:

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

但这不起作用。

谁能帮帮我?

谢谢。

【问题讨论】:

  • 看来您已经回答了自己的问题,太好了!通常最好不要编辑您的问题,而是将解决方案作为答案发布,以便其他遇到相同问题的人将来能够更轻松地找到它。

标签: node.js express passport.js


【解决方案1】:
var session = require('express-session');
var flash = require('express-flash');
var cookieParser = require('cookie-parser');
app.use(cookieParser());
app.use(session({
    secret: config.sessionKey,
    resave: true,
    saveUninitialized: true,
}));
app.use(flash());

【讨论】:

  • 通常希望用代码回答来解释代码的作用以及它解决问题的原因。
  • 对不起,只是为了完成上面的答案(ERICSONARON)
【解决方案2】:

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

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

【讨论】:

  • 我如何克服这个问题在本地机器上测试它
  • 将您的应用配置为使用会话和护照。此外,将 cookie 会话配置为安全并通过 HTTP 而不是 HTTPS 请求您的应用程序。
猜你喜欢
  • 2016-10-27
  • 1970-01-01
  • 2015-01-19
  • 2017-07-17
  • 1970-01-01
  • 1970-01-01
  • 2015-02-23
  • 2018-12-07
  • 1970-01-01
相关资源
最近更新 更多