我的應用程序允許用戶通過PassportJS爲他的帳戶設置多個oAuth連接。PassportJS:連接帳戶時是否可能無法序列化用戶?
每當我連接其他應用程序(使用Mailchimp策略和Salesforce策略)時,護照會將我註銷(結束我的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驗證自己的用戶正在改變。我可以通過檢查user.id
傳遞給passport.serializeuser()
和passport.deserializeuser()
的對象來看到這一點。但我並沒有在這裏創建一個新用戶 - 我只是將每個API返回的配置文件添加到我的原始用戶帳戶。
如何防止發生這種情況並保持原始會話的活動狀態?