javascriptfacebookfacebook-ui

How to detect user logging out of Facebook after logging into my app?


My application uses Facebook authentication:

FB.init({

    appId: config.fbAppId,
    status: true,
    cookie: true,
//  xfbml: true,
//  channelURL : 'http://WWW.MYDOMAIN.COM/channel.html', // TODO
    oauth  : true

});

// later...

FB.login(function(response)
{
    console.log(response);
    console.log("authId: " + response.authResponse.userID);
    gameSwf.setLoginFacebook(response.authResponse.accessToken);
}, {scope:'email,publish_actions,read_friendlists'});

And when using it, people can post to their wall:

var obj = {
      method: 'feed',
      link: linkUrl,
      picture: pictureUrl,
      name: title,
      caption: "",
      description: message
    };

    function callback(response) {
      // console.log("Post on wall: " + response);
    }

    FB.ui(obj, callback);

This works fine, but there is one little hickup. If people:

  1. Log in on the app.
  2. Log out of Facebook.
  3. Attempt to make a wall post from the app.

The opening of the wall post dialog fails. The console says "Refused to display document because display forbidden by X-Frame-Options.".

Can I instead get Facebook to show a login prompt to the user. Or can I detect the error and tell the user that he's no longer logged in on Facebook?


Solution

  • Just recall getLoginStatus BUT forcing a roundtrip to Facebook. Look following code:

    FB.getLoginStatus(function(response) {
      // some code
    }, true);
    

    Look the last parameter set to true to force the roundtrip.

    From JS SDK documentation:

    To improve the performance of your application, not every call to check the status of the user will result in request to Facebook's servers. Where possible, the response is cached. The first time in the current browser session that FB.getLoginStatus is called, or the JS SDK is init'd with status: true, the response object will be cached by the SDK. Subsequent calls to FB.getLoginStatus will return data from this cached response.

    This can cause problems where the user has logged into (or out of) Facebook since the last full session lookup, or if the user has removed your application in their account settings.

    To get around this, you call FB.getLoginStatus with the second parameter set to true to force a roundtrip to Facebook - effectively refreshing the cache of the response object. (http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/)