2015-12-21 15 views
1

我的應用程序允許用戶通過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返回的配置文件添加到我的原始用戶帳戶。

如何防止發生這種情況並保持原始會話的活動狀態?

回答

0

想出了一個解決這個(對誰upvoted這個問題):

我就在想它是與返回的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); 
    }) 
}); 

完成和完成的。

相關問題