我正在使用node passport-google-oauth模块,并尝试使用Custom returnUrl on Node.js Passport's Google strategy中建议的基于会话的会话成功进行身份验证后将页面重定向回原始路径。但是,我在回调中发现,会话中存储的重定向信息总是被删除,因此我被迫重定向到默认值(在这种情况下为' /')。
以下是我的代码段的日志记录输出。看来当它回调时,回归就会消失。这是护照-ogle-oauth的设计吗?如果是这样,如何在成功验证后重定向回原始页面?
//从
输出Storing url: /newroom
in /auth/google, returnTo = /newroom
in /auth/google/callback, returnTo = undefined
Storing url: /newroom
in /auth/google, returnTo = /newroom
in /auth/google/callback, returnTo = undefined
// code snippet
var express = require('express'),
[sic]
passport = require('passport'),
GoogleStrategy = require('passport-google-oauth').OAuth2Strategy,
cookieParser = require('cookie-parser'),
expressSession = require('express-session'),
bodyParser = require('body-parser');
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
passport.use(new GoogleStrategy({
clientID: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
callbackURL: 'http://' + MY_HOST + '/auth/google/callback',
},
function(accessToken, refreshToken, profile, done) {
process.nextTick(function() {
console.log('Profile:' + profile);
return done(null, profile);
});
}
));
var auth = function(req, res, next){
if (!req.session.user) {
console.log('Storing url: ' + req.url);
req.session.returnTo = req.url;
res.redirect('/auth/google');
} else if (!userIsValid(req.session.user)) {
// handle invalid user
} else {
next();
}
};
app.get('/auth/google', function(req, res, next) {
console.log("in /auth/google, returnTo = " + req.session.returnTo);
passport.authenticate('google',
{
scope: ['https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email']
}
)(req, res, next)
}
);
app.get('/auth/google/callback', function(req, res, next) {
console.log("in /auth/google/callback, returnTo = " + req.session.returnTo);
passport.authenticate('google', function(err, user, info) {
console.log("in /auth/google/callback callback");
if (err || !user) {
res.redirect('/auth/google');
} else {
var returnTo = req.session.returnTo ? req.session.returnTo : '/';
delete req.session.returnTo;
console.log('Redirecting to: ' + returnTo);
req.session.user = user;
res.redirect(returnTo);
}
})(req, res, next);
});
答案 0 :(得分:1)
我用
res.redirect(req.headers.referer);
但问题是有时候它有效它不会好像用户将第一次登录facebook,facebook将首先显示权限对话框,覆盖referer header var但是用户已经授予您的应用权限,他/她将被重新安排回到他们来的任何地方。
SBE - 抱歉英语不好;)
答案 1 :(得分:1)
只有当前未设置变量时,才能在会话中存储第一个URL。如果设置了此var,则发布登录,然后重新指向。
这比第三方oauth可能不准确的标题要好。
在Java中我会在一个过滤器中执行它(检查一个会话变量,比如redirect_to或first_url,如果它的null然后设置它,如果不是登录/注册页面)。在节点j中,可以使用中间件,如https://stackoverflow.com/a/13336055/1643558
中所述引用该答案:
我有一个与app.get('/account', auth.restrict, routes.account)
一起使用的中间件,它在会话中设置了redirect_to ...然后我重定向到/ login
auth.restrict = function(req, res, next){
if ( !req.session.userid ) {
req.session.redirect_to = '/account';
res.redirect('/login');
} else {
next();
}
};
然后在routes.login.post
我执行以下操作:
var redirect_to = req.session.redirect_to ? req.session.redirect_to : '/';
delete req.session.redirect_to;
//is authenticated ?
res.redirect(redirect_to);