[英]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.