简体   繁体   English

使用Node.js和Passport进行身份验证后,如何将用户重定向至带有其ID的主页?

[英]How to redirect user to home page with his ID after authentication using Node.js and Passport?

i have just started learning node.js so go easy on me :P 我刚刚开始学习node.js,所以对我轻松一点:P

I'm using passport to autenticate a user. 我正在使用护照给用户发送密码。 The user needs to be redirected to his home page with his ID as a URL parameter after successful authentication, for example: 成功认证后,需要使用ID作为URL参数将用户重定向到其主页,例如:

 /home?id=325346546

Here is a part of my routes.js 这是我的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
    }));

this is my idea, i want to pass the id as parameter to the URL. 这是我的主意,我想将id作为参数传递给URL。

I have tried putting a 我试过放一个

function(req, res) {
}

but it didn't work. 但这没用。 Any help will be appreciated. 任何帮助将不胜感激。

Thanks to Ben i got it working, so to anwser my own question here is the complete code: 多亏了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);
    });

暂无
暂无

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

相关问题 如何使用Node.js将护照身份验证中的HTML页面重定向到angularjs - How to redirect to html page in passport authentication with Node.js to angularjs 使用node.js和护照进行身份验证后重定向到上一页 - Redirecting to previous page after authentication using node.js and passport Node.js使用护照进行用户身份验证 - Node.js user authentication using passport 使用passport.js在node.js中进行身份验证后重定向到上一页 - Redirecting to previous page after authentication in node.js using passport.js 如何通过搜索他的 ID 来获取整个用户 - NODE.JS - How to get an entire User by searching for his ID - NODE.JS 在Node.js中使用Passport进行Facebook身份验证 - Facebook authentication using Passport in Node.js Node.js Express和Passport.js身份验证后,res.redirect无法正常工作 - res.redirect not working after Node.js Express & Passport.js authentication 如何检查用户Active(1)是否不使用通行证(node.js)? - How to check if the user Active(1) or not using passport(node.js)? 在社交注册后如何使用从这些社交网站[passport,node.js]获得的数据重定向用户以完成注册过程 - how to redirect a user to complete the registration process after social signup with the data obtained from those socials, [passport,node.js] 使用数据库验证登录数据后,如何将用户重定向到“主页”? - How do I redirect user to 'home' page after validating his login data with database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM