0

我的网站正在使用PHP SDK来生成登录按钮JavaScript SDK来生成注销按钮。这是因为我需要在服务器端登录按钮,所以它需要重定向到其他位置。注销后删除Facebook登录按钮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-登录按钮。”)空()在没有授权,而且在。注销后。

但是,由于竞争条件,这并不总是删除按钮。任何想法在哪里把空()函数,以避免竞争条件?

问候

+0

更换

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

你为什么不能只需输出注销按钮server-sid即可只有当有登录用户时才有e? – CBroe 2015-03-14 00:10:13

+0

@CBroe,服务器无法抓住客户端会话。它只能抓取从服务器端生成的会话。例如,用户从前一个或其他会话(可能直接来自Facebook)登录到Facebook,只有客户端或可捕获会话的设备。当服务器试图抓取该会话时,它不会获得授权。或者,也许我错了? – ethereal1m 2015-03-15 05:55:47

回答

0

创建注销按钮调用FB.logout():

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

然后用

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