簡體   English   中英

PassportJS:連接帳戶時是否可以不序列化用戶?

[英]PassportJS: Is it possible to not serialize a user when connecting an account?

我的應用程序允許用戶通過PassportJS建立與其帳戶的多個oAuth連接。

每當我連接另一個應用程序(使用Mailchimp策略和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的事情。 我可以通過檢查user.id對象被傳遞給passport.serializeuser()passport.deserializeuser()來看到。 但是我不是在這里結交新用戶-我只是將每個API返回的配置文件添加到我的原始用戶帳戶中。

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

提出了一個解決方案(對於提出這個問題的人):

我認為這與返回的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);
    })
});

做完了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM