'Facebook SDK : logging out automatically

I need to build a page that when loaded logs out automatically the user from facebook

i tried this code

    window.fbAsyncInit = function() {
    FB.init({
        appId      : 'abc123456789',
        xfbml      : false,
        version    : 'v2.7',
        status     :  true
    });
    FB.logout();
};

(function(d, s, id){
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

but it does not work. It seems that when FB.logout() is called, the FB api doesn't know the user's session.

If I call FB.logout() after an arbitrary period of time

setTimeout(function(){
     FB.logout();
}, 3000);

It works. Is there a way to know when the user information loading has been completed?



Solution 1:[1]

Use FB.getLoginStatus to check if the user is logged in. It should work to call FB.logout in the callback function of FB.getLoginStatus: https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus

For example:

FB.init({
    appId      : 'abc123456789',
    xfbml      : false,
    version    : 'v2.7',
    status     :  true
});
FB.getLoginStatus((response) => {
    if (response.status === 'connected') {
        FB.logout();
    }
}, true);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1