使用 Passport js failureRedirect 方法发回数据

2023-11-30

我有一个 Passport js 本地注册策略,它使用 successRedirect 和 failureRedirect 方法。问题是,如果注册过程出现错误,Passport 只会重定向回注册表单,表单中没有任何数据。

app.post('/signup', passport.authenticate('local-signup', {
        successRedirect: '/signup/avatar', // redirect to the secure profile section
        failureRedirect: '/signup', // redirect back to the signup page if there is an error
        failureFlash: true // allow flash messages
    }));

我的本地注册策略中有以下条件,如果提供的两个密码不匹配,则会向用户发送失败消息。

if(req.body.password != req.body.confirm_password){
                                console.log('Passwords do not match');
                                return done(null, false, req.flash('signupMessage', 'Your passwords do not match'));
                            }

除了闪现消息之外,我还想将数据发送回 /signup 路由,并使用用户提供的数据重新填充表单。

有没有办法在失败时将表单数据发送回浏览器?


答案比我想象的要简单得多。

验证应该在数据发送到 Passport js 之前进行,然后如果数据无效,可以重新渲染 /signup 模板并将数据传回。

解决方案是在这个问题中:带护照的 NodeJS 快速验证器

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Passport js failureRedirect 方法发回数据 的相关文章

随机推荐