Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.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
如何在使用Node.js和Passport进行身份验证后将用户的ID重定向到主页?_Node.js_Authentication_Login_Routes_Passport.js - Fatal编程技术网

如何在使用Node.js和Passport进行身份验证后将用户的ID重定向到主页?

如何在使用Node.js和Passport进行身份验证后将用户的ID重定向到主页?,node.js,authentication,login,routes,passport.js,Node.js,Authentication,Login,Routes,Passport.js,我刚刚开始学习node.js,所以请对我放松:p 我正在使用passport对用户进行身份验证。 成功身份验证后,需要将用户ID作为URL参数重定向到其主页,例如: /home?id=325346546 这是我的routes.js的一部分 // process the login form app.post('/', passport.authenticate('local-login', { successRedirect : '/home?id='+req

我刚刚开始学习node.js,所以请对我放松:p

我正在使用passport对用户进行身份验证。 成功身份验证后,需要将用户ID作为URL参数重定向到其主页,例如:

 /home?id=325346546
这是我的routes.js的一部分

    // process the login form
    app.post('/', passport.authenticate('local-login', {
        successRedirect : '/home?id='+req.user._id, //error because 'req' isn't declared
        failureRedirect : '/',
        failureFlash : true
    }));
这是我的想法,我想把id作为参数传递给URL

我试着把一个

function(req, res) {
}
但它不起作用。
任何帮助都将不胜感激。

感谢Ben,我让它工作了,因此,为了回答我自己的问题,这里是完整的代码:

    // process the login form
    app.post('/', function(req, res, next) {
      passport.authenticate('local-login', {failureFlash:true}, function(err, user, info) {
       if (err) { return next(err); }
       if (!user) { return res.redirect('/'); }
      req.logIn(user, function(err) {
        if (err) { return next(err); }
       return res.redirect('/home?id=' + user._id);
     });
    })(req, res, next);
    });

查看如何实现自定义回调。谢谢Ben!我让它工作:)本,我需要添加“failureFlash:true”到我的回答中吗?是的。您应该能够在回调之前将其作为选项传递<代码>app.post('/',passport.authenticate('local-login',{failureFlash:true}),函数(req,res,next){…好的,非常感谢!!