我的应用程序允许用户通过oAuth与他的帐户建立多个PassportJS连接。
每当我连接另一个应用程序(使用Mail猩猩策略和Salesforce策略),Passport就会登录我(结束我的express-session)。看起来Passport正在尝试为我所连接的每个策略使用一个新的会话登录,这完全不是我想要的。
我认为秘诀在于我正在使用的策略回调,即返回的done()函数:
passport.use(new MailChimpStrategy({
clientID: auth.mailchimpAuth.clientID,
clientSecret: auth.mailchimpAuth.clientSecret,
callbackURL: auth.mailchimpAuth.callback
}, function(accessToken, refreshToken, profile, done) {
process.nextTick(function() {
Chimp.findOne({'login_name': profile._json.login.login_name},
function(err, chimp) {
if (err) {
return done(err);
}
if (!chimp) {
var newChimp = new Chimp();
newChimp.login_name = profile._json.login.login_name;
newChimp.profile = profile;
newChimp.authUsers = [];
newChimp.domain = '';
newChimp.accessToken = accessToken;
newChimp.lists = {};
newChimp.save(function(err) {
if (err) {
return done(err);
} else {
return done(null, newChimp);
}
});
} else {
var newChimp = chimp;
return done(null, newChimp);
}
});
});
}));这大概是因为当我使用一个新的API进行身份验证时,我的用户正在更改Passport内容。我可以通过检查传递给passport.serializeuser()和passport.deserializeuser()的passport.serializeuser()对象来看到这一点。但我不是在这里创建一个新用户-我只是将每个API返回的配置文件添加到我的原始用户帐户中。
如何防止这种情况发生,并使原始会话保持活动状态?
发布于 2015-12-22 00:54:27
想出了解决这个问题的办法(对于那些投票赞成这个问题的人来说):
我认为这与被返回的done()有关,这是正确的;所发生的情况是,done()将一个对象返回给serializeUser(),然后将对象传递给deserializeUser()。
因此,在我的mailChimpStrategy函数中,我添加了passReqToCallback: true,然后从回调函数访问登录用户:
passport.use(new MailChimpStrategy({
clientID: auth.mailchimpAuth.clientID,
clientSecret: auth.mailchimpAuth.clientSecret,
callbackURL: auth.mailchimpAuth.callback,
passReqToCallback: true
}, function(req, accessToken, refreshToken, profile, done) {
var tempUser = {id: req.session.passport.user}
User.findById(tempUser.id, function(err, usr){
var newChimp = new Chimp(); // Make new Mongoose Chimp object
// Do Chimp profile setting here
// Then, when done, instead of passing the Chimp profile object, I pass the user ID.
return done(null, tempUser);
})
});已经做好了。
https://stackoverflow.com/questions/34405868
复制相似问题