簡體   English   中英

在Facebook上登錄時Facebooker2 current_facebook_user無

[英]Facebooker2 current_facebook_user nil when logged in on Facebook

我一直在嘗試使用Facebooker2作為在Rails中通過Facebook登錄的方法。 但是,即使我確實登錄了Facebook,Facebooker2的current_facebook_user函數還是以某種方式返回nil。 有任何想法嗎? 謝謝!

這是我的代碼的要點:

在我的一個控制器中:

def check_login
    if current_facebook_user.nil?
        "This always gets returned, even though I'm logged into Facebook."
    else
        "This never gets run"
    end
end

Facebook JS。 FB.Event.subscribe('auth.login', function(response) { ... }重定向到上面的check_login函數。

<script>
    window.fbAsyncInit = function() {
        FB.init({
            appId : '123456789012345',
            status : true,  // check login status
            cookie : true,  // enable cookies to allow the server to access the session
            channelUrl : 'true',  // add channelURL to avoid IE redirect problems
            xfbml : true  // parse XFBML
        });
        FB.Event.subscribe('auth.login', function(response) {
            window.location = "http://www.mysite.com/check_login";
        });
    }; ( function() {
        var s = document.createElement('div');
        s.setAttribute('id', 'fb-root');
        document.documentElement.getElementsByTagName("body")[0].appendChild(s);
        var e = document.createElement('script');
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        e.async = true;
        s.appendChild(e);
    }());
    function logout() {
        FB.logout();
        window.location = 'http://www.mysite.com/logout';
    };
</script>

如果您使用Facebook JavaScript SDK通過瀏覽器登錄,則用戶會話數據將保存在瀏覽器中。 結果,您的Rails代碼不會意識到用戶狀態的更改。 因此,您必須將有效的當前用戶會話數據顯式傳遞回您的服務器,在該服務器上您還可以維護有效的會話服務器端。

例如:

    FB.Event.subscribe('auth.login', function(response) {
        window.location = "http://www.mysite.com/check_login?session=" + JSON.stringify(response.session);
    });

暫無
暫無

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

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