简体   繁体   English

Node.js护照将postdata传递给failureRedirect

[英]Node.js passport pass postdata on to failureRedirect

This is driving me crazy! 这真让我抓狂! I'm using Express 4, passport and local-passport to authenticate login and signup. 我正在使用Express 4,护照和本地护照来验证登录和注册。

I'm using this example: https://github.com/tutsplus/passport-mongo 我正在使用此示例: https : //github.com/tutsplus/passport-mongo

Problem: When the signup form does not validate (say you forgot one of the fields) and we redirect to the failureRedirect (which is the same signup page), all the entered values are gone. 问题:当注册表单未通过验证(例如您忘记了其中一个字段)并且我们重定向到了failureRedirect(与同一注册页面相同)时,所有输入的值都消失了。 Not a very good user experience that you have to fill out the entire form because you messed up a single field. 您不是一个很好的用户体验,因为您弄乱了一个字段,所以必须填写整个表单。

How do I pass the already entered data on the the form? 如何在表格上传递已经输入的数据?

I got these two routes handing the GET and POST of the form: 我得到了以下两种方式来处理表单的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
}));

I have a nagging suspicion that the values are already there - I just don't know how to grab them. 我有点怀疑值已经存在-我只是不知道如何获取它们。

I'm using Swig for the views btw. 我正在使用Swig的视图。

Did you add connect-flash middleware to your app? 您是否向应用程序添加了连接闪存中间件?

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

app.use(flash());

Update: 更新:

When you define your local strategy, you should return the flash messages in the third parameter of the done function. 定义本地策略时,应在done函数的第三个参数中返回Flash消息。 Example: 例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM