注销后删除Facebook登录按钮SDK

时间:2015-03-13 19:13:43

标签: javascript jquery facebook facebook-javascript-sdk facebook-php-sdk

我的网站正在使用PHP SDK生成登录按钮Javascript SDK以生成注销按钮。这是因为我需要在服务器端登录按钮,因此需要重定向到其他位置。

注销按钮需要位于应用程序端,因为服务器无法获取先前创建的会话。

当没有人登录网站时,假设PHP登录按钮出现,而Javascript登录按钮假设使用jquery empty()函数消失。

大多数情况下,解决方案有效,但显然存在竞争条件,其中Javascript按钮未被删除,我无法找出原因。

// This is called with the results from from FB.getLoginStatus().
            function statusChangeCallback(response) {
                console.log('statusChangeCallback');
                console.log(response);
                // The response object is returned with a status field that lets the
                // app know the current login status of the person.
                // Full docs on the response object can be found in the documentation
                // for FB.getLoginStatus().
                if (response.status === 'connected') {
                    // Logged into your app and Facebook.
                    testAPI();
                } else if (response.status === 'not_authorized') {
                    // The person is logged into Facebook, but not your app.
                        $(\".fb-login-button\").empty();
                } else {
                    // The person is not logged into Facebook, so we're not sure if
                    // they are logged into this app or not.
                    $(\".fb-login-button\").empty();
                }
            }

            // This function is called when someone finishes with the Login
            // Button.  See the onlogin handler attached to it in the sample
            // code below.
            function checkLoginState() {
                FB.getLoginStatus(function(response) {
                    statusChangeCallback(response);
                });
            }

            window.fbAsyncInit = function() {
                FB.init({
                appId      : '".FB_APP_ID."',
                xfbml      : true,
                version    : 'v2.2'
                });

                FB.Event.subscribe(\"auth.logout\", function() {
                    $(\".fb-login-button\").empty();
                    $(\"#facebook-message\").empty();
                });

以下是按钮标记:

<div class="fb-login-button" data-max-rows="1" data-size="xlarge"  data-show-faces="false" data-auto-logout-link="true"></div>   

如上所述,我调用$(“。fb-login-button”)。empty(),其中没有授权,注销后。

但由于竞争条件,这并不总是删除按钮。有什么想法放空()函数以避免竞争条件吗?

问候

1 个答案:

答案 0 :(得分:0)

我创建了一个调用FB.logout()的注销按钮:

<button id="facebook-logout-button" onclick="FB.logout();" >Facebook logout</button>

然后替换

$(\".fb-login-button\").empty();

$("#facebook-logout-button").remove();