2010-10-01 108 views
0

我拼命試圖在用戶開始新的Facebook會話之前強制註銷。但在註銷成功呼叫之前,auth.login似乎登錄用戶並重定向它們。所以這似乎是一個競爭條件,但我無法弄清楚如何繞過它。使用FB.logout避免在Facebook JavaScript API中競爭條件

FB.init({appId: xxxx, status: true, cookie: true, xfbml: true}); 

    //Try to log out user 
    FB.logout(); 

      //Show the FB div 
      if(FB) { 
       FB.Event.subscribe('auth.logout', function(response) { 
        // do something with response 
        logout(); 
       }); 
       FB.getLoginStatus(function(response) { 
        if (response.session) { 
         login(response); 
        } 
       }); 
       FB.Event.subscribe('auth.login', function(response) { 
        login(response); 
        //login redirects the user. Before logout fires() 
       }); 

      } // end if(FB). 

回答

2

事情是這樣的,也許:

//initial login check 
FB.getLoginStatus(function(response) { 
    if (response.session) { 
     //logged in, force logout 
     FB.logout(function() { 
      //logged out, subscribe to events 
      loginEvents(); 
     }); 
    } else { 
     //not logged in 
     loginEvents(); 
    } 
}); 

function loginEvents() { 
    FB.Event.subscribe('auth.logout', function(response) { 
     // do something with response 
     logout(); 
    }); 

    FB.Event.subscribe('auth.login', function(response) { 
     login(response); 
     //login redirects the user. Before logout fires() 
    }); 
}