0

我一直在尝试使用 Facebooker2 作为在 Rails 中通过 Facebook 登录的一种方式。但是,Facebooker2 的 current_facebook_user 函数以某种方式返回 nil,即使我肯定已登录 Facebook。有任何想法吗?谢谢!

这是我的代码的要点:

在我的一个控制器中:

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 代码不知道用户状态的变化。因此,您必须将有效的当前用户会话数据显式传递回您的服务器,您也可以从中维护有效的会话服务器端。

例如:

    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 回答