注销后fb.getloginstatus没有触发

时间:2016-01-10 12:53:06

标签: javascript facebook-javascript-sdk facebook-oauth

我长期使用Facebook OAuth。

应用程序没有得到getLoginStatus总是处于未知状态。最近我遇到了这个问题。

工作正常,直到我点击退出按钮。一旦我点击退出按钮我面对此登录
问题

这是我的代码

<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>

1 个答案:

答案 0 :(得分:0)

谢谢大家,我找到了解决这个问题的方法

删除注销时的所有Cookie。

我用它来删除cookie

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