Search code examples
javascriptfacebook-javascript-sdkfacebook-oauth

fb.getloginstatus not firing after logout


I am using Facebook OAuth from longtime.

the App not getting getLoginStatus always getting unknown status.recently i got this problem.

works fine until i click logout button.Once i click logout button i face this login
problem.

This is my code

<html>

<head>
</head>

<body>
    <div id="fb-root">
        <br>
        <br>
        <button onclick="status();">Login</button>
        <br>
        <br>
        <button onclick="logout();">Log Out</button>
    </div>
    <div id="fb-root"></div>
    <script>
    window.fbAsyncInit = function() {
        FB.init({
            appId: '137827113065883',
            status: true,
            cookie: true,
            xfbml: true,
        });

        FB.Event.subscribe('auth.login', function(response) {
            alert('logged in');
        });

        FB.Event.subscribe('auth.logout', function(response) {
            alert('logged out');
        });
    };

    function logout() {
        FB.logout(function(response) {
            console.log('out ' + JSON.stringify(response));
        });
    }

    function status() {
        FB.getLoginStatus(function(response) {
            console.log(JSON.stringify(response))
            if (response.status === 'connected') {
                var accessToken = response.authResponse.accessToken;
                console.log(accessToken);
            } else if (response.status === 'not_authorized') {
                var redirect_uri = window.location.protocol + "//" + window.location.host + window.location.pathname;

                window.location = "https://facebook.com/dialog/oauth?redirect_uri=" + redirect_uri + "&client_id=137827113065883&response_type=token";
            } else {
                var redirect_uri = window.location.protocol + "//" + window.location.host + window.location.pathname;

                window.location = "https://facebook.com/dialog/oauth?redirect_uri=" + redirect_uri + "&client_id=137827113065883&response_type=token";
            }
        }, true);
    }

    //Load the SDK Asynchronous
    (function(d) {
        var js, id = 'facebook-jssdk',
            ref = d.getElementsByTagName('script')[0];
        if (d.getElementById(id)) {
            return;
        }
        js = d.createElement('script');
        js.id = id;
        js.async = true;
        js.src = "//connect.facebook.net/en_US/all.js";
        ref.parentNode.insertBefore(js, ref);
    }(document));
    </script>
    </body>

</html>

Solution

  • Thanks guys i found a workaround for this problem

    by deleting all cookies on logout click.

    i am using this for deleting cookies

    document.cookie.split(";").forEach(function(c) {
                    document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";domain=.example.com;path=/");
                });