Node.js Passport上的自定义返回URL';谷歌的战略
我使用的是Express和Passport OpenID Google策略,我想在每个身份验证请求上设置returnURL,以便能够返回到启动该身份验证的页面 现在的情况是,我有一个HTML5幻灯片应用程序,它的后端是Node.js(还有社交素材、编辑器、门户和扩展…),我希望能够让用户登录到某张幻灯片上(通过幻灯片菜单…),但我希望他能够轻松地回到同一张幻灯片上 所以我需要这样的东西Node.js Passport上的自定义返回URL';谷歌的战略,node.js,express,passport.js,Node.js,Express,Passport.js,我使用的是Express和Passport OpenID Google策略,我想在每个身份验证请求上设置returnURL,以便能够返回到启动该身份验证的页面 现在的情况是,我有一个HTML5幻灯片应用程序,它的后端是Node.js(还有社交素材、编辑器、门户和扩展…),我希望能够让用户登录到某张幻灯片上(通过幻灯片菜单…),但我希望他能够轻松地回到同一张幻灯片上 所以我需要这样的东西 app.get('/auth/google', function(req,res) { var cust
app.get('/auth/google', function(req,res) {
var cust = "http://localhost:1338/"+req.params.xxx;
passport.authenticate('google', returnURL:cust, function ...
}
我读过护照指南,但仍然不知道怎么做。我知道这不安全,但我还能怎么做呢
或者如何使应用程序返回到启动登录的页面?或者有没有一种方法可以使用AJAX进行OpenID身份验证(并且仍然可以使用passport) 尝试
res.redirect('back')
在passport.authenticate的回调中,我已经为我的应用程序Twitter身份验证解决了这个问题,我确信Google的策略非常类似。尝试以下一种变体:
假设您已从身份验证服务(如passport指南中)定义回调的路由:
只需将该块更改为:
app.get('/auth/twitter/callback', function(req, res, next){
passport.authenticate('twitter', function(err, user, info){
// This is the default destination upon successful login.
var redirectUrl = '/account';
if (err) { return next(err); }
if (!user) { return res.redirect('/'); }
// If we have previously stored a redirectUrl, use that,
// otherwise, use the default.
if (req.session.redirectUrl) {
redirectUrl = req.session.redirectUrl;
req.session.redirectUrl = null;
}
req.logIn(user, function(err){
if (err) { return next(err); }
});
res.redirect(redirectUrl);
})(req, res, next);
});
现在,为经过身份验证的路由定义中间件,以便在会话中存储原始URL,如下所示:
ensureAuthenticated = function (req, res, next) {
if (req.isAuthenticated()) { return next(); }
// If the user is not authenticated, then we will start the authentication
// process. Before we do, let's store this originally requested URL in the
// session so we know where to return the user later.
req.session.redirectUrl = req.url;
// Resume normal authentication...
logger.info('User is not authenticated.');
req.flash("warn", "You must be logged-in to do that.");
res.redirect('/');
}
工作 根据作者的说法,这在OpenID策略中是不可能的。我们通过直接访问变量来动态更新这些变量:
app.get('/auth/google', function(req, res, next) {
passport._strategies['google']._relyingParty.returnUrl = 'http://localhost:3000/test';
passport._strategies['google']._relyingParty.realm = 'http://localhost:3000';
passport.authenticate('google')(req, res, next);
});
只要有登录按钮,就将请求的当前URL附加为 查询参数(根据您使用的模板系统进行调整): 最后,在回调处理程序中,重定向到会话中存储的URL:
app.get('/auth/google/callback', passport.authenticate('google',
failureRedirect: '/'
), function (req, res) {
res.redirect(req.session.redirect || '/');
delete req.session.redirect;
});
哈,谢谢!显然,反向路由设置在第一个和第二个OpenID请求之间。我不知道!谢谢,我发现这个有问题。只有在我已经使用谷歌登录的情况下,它才有效。如果passport重定向到Google页面,然后返回到我的站点,“页面”路径将丢失。有没有其他方法可以迫使它从谷歌返回到我的位置(登录开始的页面)?@bubersson如果我理解正确,你需要使用一种更主动的方法,比如第一个答案上的方法。这非常有效,但在重定向时,如果用户被发送出去进行身份验证并返回,则它似乎缺少散列参数。登录后重定向时,是否有办法将这些内容保留在URL中@用户645715可能尝试使用
req.session.redirectUrl=req.originalUrl代码>或者req.url
是否可以在没有会话的情况下执行此操作?
<a href='/auth/google?redirect=<%= req.url %>'>Log In</a>
app.get('/auth/google', function(req, res, next) {
req.session.redirect = req.query.redirect;
next();
}, passport.authenticate('google'));
app.get('/auth/google/callback', passport.authenticate('google',
failureRedirect: '/'
), function (req, res) {
res.redirect(req.session.redirect || '/');
delete req.session.redirect;
});