Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 重定向passport.authenticate()内部时出现问题_Javascript_Node.js_Passport.js - Fatal编程技术网

Javascript 重定向passport.authenticate()内部时出现问题

Javascript 重定向passport.authenticate()内部时出现问题,javascript,node.js,passport.js,Javascript,Node.js,Passport.js,我的passport js身份验证有问题。在我的passport.authenticate()回调中调用res.redirect('/')时,尝试访问未定义的属性时出错。我的身份验证策略如下所示: passport.use(new LocalStrategy({ session: false }, (username, password, done) => { console.log('strategy invoked'); let user = new User('jason

我的passport js身份验证有问题。在我的
passport.authenticate()回调中调用
res.redirect('/')
时,尝试访问未定义的属性时出错。我的身份验证策略如下所示:

passport.use(new LocalStrategy({
  session: false
}, (username, password, done) => {
  console.log('strategy invoked');
  let user = new User('jason', 'password');
  return done(null, user);
}));
我的路线管理员:

app.post('/auth', 
passport.authenticate('local', { failureRedirect: '/fail'},
function(req, res){
    console.log('authenticate callback');
    console.log(req.body);   //logs undefined
    res.redirect('/');       //throws error here
  }
));
因此,当我使用curl向/auth路由发送帖子时,我得到一个错误,上面写着:
cannotreadproperty';重定向';未定义的属性
。我尝试了路由处理程序的多种变体,包括我刚刚将successRedirect设置为“/”但从未触发“/”的路由处理程序的变体。我也试过这个:

app.post('/auth', (req, res) => {
passport.authenticate('local', (req, res) => {
    console.log('authenticate callback');
    console.log(req.body);
    res.redirect('/');
  })(req, res);
});
所以我的问题是:

  • 如何访问身份验证回调中的req和res对象,以便 读取req正文并发送响应,例如res.redirect('/')

  • 如果设置
    successRedirect:“/”
    是一个更好的选项,我如何确保我的重定向 他们真的被跟踪了吗


  • 您的代码中有一个bug。您将passport中间件和回调函数组合到同一个参数中。请尝试下面的代码或查看文档中的示例:


    哇,真不敢相信我竟然没注意到。谢谢
    app.post('/auth', passport.authenticate('local', { failureRedirect: '/fail'}), function(req, res){
        console.log('authenticate callback');
        console.log(req.body);   //logs undefined
        res.redirect('/');       //throws error here
    });