Search code examples
facebookfacebook-authentication

auth.statusChange doesn't fire during FB.init if the user isn't logged in to facebook


I wondered if anyone has found a workaround to the behaviour that I'm experiencing.

Take the example of the below code:

<script>
window.fbAsyncInit = function() {
        FB.init({
          appId      : '[----removed-------]', // App ID
          channelUrl : 'http://localhost/channel.html', // Channel File
          status     : true, // check login status
          cookie     : true, // enable cookies
          xfbml      : true  // parse XFBML
        });     
        // Additional initialization code here

        FB.Event.subscribe('auth.statusChange',fbLoginStatus);

        console.log("getloginstatus");

        FB.getLoginStatus(fbLoginStatus);
</script>

Using FB.init with status set to true, essentially calls getLoginStatus() according to the facebook documentation. However, it is apparently intended behaviour that this will not trigger the event auth.statusChange, because the default value is 'unknown', and the value for 'not logged in' is also 'unknown' (even though it can be known!!)

Therefore I have had to make a call to FB.getLoginStatus() explicitly as well as having the status set to true if I want to also respond to users who are not logged in to facebook.

The problem is, this results in the function being called twice if the user is anything other than "logged out".

Is there a neat way to stop this happening? I think my only option might be to call a different function to handle auth change events..


Solution

  • My best solution for this was to set status to 'false' in the fb.init options, then to explicitly call getloginstatus separately.

    IF get loginstatus came back as unknown (i.e. logged out), I subscribed to the status change event, as well as doing the usual of displaying the login button. Then when the user logs in, status change fires as expected.