Log user out of the facebook when user logs out of the application.

As per the facebook policies we need to log the user out of the facebook when he clicks sign\_out in the app.

To achieve this there are many ways, some of them got deprecated. One way that is suggested by facebook is to use their [Facebook Javascript SDK (Facebook Javascript sdk)][1]

Codes:

[Logout of facebook in rails(Logout of facebook in rails)][2]

I used following code in document ready to achieve this functionality.

  window.fbAsyncInit = function() {

    FB.init({
        appId: window.fb_app_id,
        status: true,
        cookie: true,
        xfbml: true,
        oauth: true
    });

    $('#sign_out').click(function(e) {
        FB.getLoginStatus(function(response) {
            if (response.authResponse) {
                 FB.logout(function(response) {
                    window.location.replace($('#sign_out').attr("href"))
                  });
            }
        });
        return false;
    });
};
(function(d) {
    var js, id = 'facebook-jssdk';
    if (d.getElementById(id)) {
        return;
    }
    js = d.createElement('script');
    js.id = id;
    js.async = true;
    js.src = "//connect.facebook.net/en_US/all.js";
    d.getElementsByTagName('head')[0].appendChild(js);
}(document));

Few points regarding this code:

        FB.logout(function(response) {
         window.location.replace($('#sign_out').attr("href"))
        });

The url which we pass here is the url that points to actual signout of the app and that sould be handled in the callback. otherwise the user will be logged out of the app but not from the facebook due to async nature of the FB.logout()

Note: View should contain a div with id as "fb-root" for the initialization process.
[1]: http://developers.facebook.com/docs/reference/javascript/
[2]: http://pastebin.com/hfqtEXWG