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();