带护照的Express flash消息不起作用

时间:2015-03-27 21:41:01

标签: 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
}));

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

但这不起作用。

任何人都可以帮助我?

感谢。

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:0)

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());