0

RailsでFacebook経由でログインする手段としてFacebooker2を使用しようとしています。ただし、Facebook に確実にログインしているにもかかわらず、Facebooker2 の current_facebook_user 関数はどういうわけか nil を返しています。何か案は?ありがとう!

これが私のコードの要点です:

私のコントローラーの1つで:

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

フェイスブック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>
4

1 に答える 1

1

Facebook JavaScript SDK を使用してブラウザーからログインすると、ユーザーのセッション データはブラウザー内に保持されます。その結果、Rails コードは us​​ers 状態の変化を認識しません。そのため、有効な現在のユーザー セッション データをサーバーに明示的に渡す必要があり、そこから有効なセッション サーバー側も維持できます。

例えば:

    FB.Event.subscribe('auth.login', function(response) {
        window.location = "http://www.mysite.com/check_login?session=" + JSON.stringify(response.session);
    });
于 2011-08-03T16:23:22.827 に答える