我试图让这个工作,但我迷路了。我想将成功或失败的人重定向到提供的 URL。
当他们请求身份验证时,我将 URL 设置到服务器上的会话中。
// Setup redirect URLs
if (typeof(req.session) !== 'undefined') {
if (typeof(req.session.passURL) == 'undefined') {
req.session.passURL = '';
}
if (typeof(req.session.failURL) == 'undefined') {
req.session.failURL = '';
}
if (typeof(req.query.passURL) != 'undefined') {
req.session.passURL = req.query.passURL;
}
if (typeof(req.query.failURL) != 'undefined') {
req.session.failURL = req.query.failURL;
}
} else {
console.log('No Session?!?');
}
然后我向 oAuth 提供者请求身份验证:
console.log(req.session);
req.app.passport.use(googleStrategy);
req.app.passport.authenticate('google', {
scope: [
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email'
]
})(req, res);
上面的会话日志给了我:
{ cookie:
{ path: '/',
_expires: null,
originalMaxAge: null,
httpOnly: true,
proxy: true,
secure: true,
key: 'cookie.sid' },
passport: {},
passURL: 'google.com',
failURL: '' }
当谷歌完成并将用户发送回回调 URL 时,我会这样做:
console.log(req.session);
req.app.passport.authenticate('google', {
failureRedirect: req.session.failURL
})(req, res, function(err) {
if (err) {
req.app.lib.util.returnJSON(req, res, 200, err);
} else {
getUser(req, req.session.passport.user, function () {
req.session.passport = null;
if (typeof(req.session.passURL) != 'undefined' && req.session.passURL != '') {
res.redirect(req.session.passURL);
} else {
req.app.lib.util.returnJSON(req, res, 200, JSON.stringify(req.session.user));
}
});
}
});
在 oAuth 回调中的日志会话中,我得到:
{ cookie:
{ path: '/',
_expires: null,
originalMaxAge: null,
httpOnly: true,
proxy: true,
secure: true,
key: 'cookie.sid' },
passport: {} }
根据 Chrome,这是客户端看到的过程:
节点似乎已正确设置会话:
app.use(express.session({
store: new MongoStore({db: connection}),
cookie: {
proxy: true,
maxAge: null,
secure: true,
key: 'cookie.sid'
},
secret: app.get('cookie_secret')
}));
app.use(app.passport.initialize());
app.use(app.passport.session());
app.use(app.router);
我不明白为什么会话会丢失。