【问题标题】:Node.js passport pass postdata on to failureRedirectNode.js 护照将 postdata 传递给 failureRedirect
【发布时间】:2014-10-16 03:58:09
【问题描述】:

这快把我逼疯了! 我正在使用 Express 4、护照和本地护照来验证登录和注册。

我正在使用这个例子: https://github.com/tutsplus/passport-mongo

问题: 当注册表单未通过验证(例如您忘记了其中一个字段)并且我们重定向到 failureRedirect(这是同一个注册页面)时,所有输入的值都消失了。您必须填写整个表单,因为您搞砸了一个字段,这不是一个很好的用户体验。

如何在表单上传递已经输入的数据?

我得到了这两条路线来处理表单的 GET 和 POST:

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

app.post('/signup', passport.authenticate('signup', {
    successRedirect: '/signup-complete',
    failureRedirect: '/signup', // need to pass the entered values (if any) back to the signup page
    failureFlash : true
}));

我一直怀疑这些值已经存在 - 我只是不知道如何获取它们。

顺便说一句,我正在使用 Swig 获取视图。

【问题讨论】:

  • 我也遇到了同样的问题,请问您现在有什么解决办法吗?

标签: node.js passport.js


【解决方案1】:

您是否将connect-flash 中间件添加到您的应用中?

var flash = require('connect-flash');

app.use(flash());

更新:

当您定义本地策略时,您应该在 done 函数的第三个参数中返回 flash 消息。示例:

passport.use(new LocalStrategy(
  function(username, password, done) {
      findByUsername(username, function(err, user) {
        if (err) { return done(err); }
        if (!user) { return done(null, false, { message: 'Unknown user ' + username }); }
        if (user.password != password) { return done(null, false, { message: 'Invalid password' }); }
        return done(null, user);
      })
    });
  }
));

【讨论】:

  • 是的,我正在使用 flash 中间件,但是在调用 failureRedirect 时如何刷新值?似乎护照只接受这里的路线。理想情况下,我想要一个中间函数,然后进行重定向。
  • 然后可以从flash('error')找到设置这种正确方式的消息。
  • 按预期工作,不需要太多体操
猜你喜欢
  • 2017-11-21
  • 2018-05-26
  • 2015-07-01
  • 2022-08-04
  • 2015-05-25
  • 1970-01-01
  • 1970-01-01
  • 2018-12-29
  • 2018-09-14
相关资源
最近更新 更多