views:

70

answers:

1

I am desperately trying to force a logout before a user starts a new facebook session. But before logout is succesfully called, auth.login seems to login the user and I redirect them. So it seems to be a race condition, but I can't figure out how to get around it.

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).
+1  A: 

Something like this maybe:

//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()
    });
}
serg