11

When I click on logout... I get this exception in console:

FB.logout() called without an access token.

Question: I already have Access token saved in Session and I have access to it in Client side also. I already checked this answer and could not understand that how can I invoke handleSessionResponse method and how what will be the response value when I click on logout button? https://stackoverflow.com/a/8430670/726802

I have below code in layout page(master page).

<script>
    window.fbAsyncInit = function() {
        FB.init({
            appId      : '{!! env("FACEBOOK_APP_ID") !!}',
            xfbml      : true,
            version    : '{!! env("Facebook_Version") !!}'
        });
        FB.AppEvents.logPageView();
    };
    (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'));
</script>

Below is the code in blade(content page)

<ul class="nav nav-sidebar">
    <li class="active"><a href="#" onclick="logout();">Logout</a></li>
</ul>

<script>
    function checkLoginState() {
        FB.getLoginStatus(function(response) {
            if (response.status === 'connected') {

            } else {
            }
        });
    }

    function logout() {
        debugger;
        FB.logout(function(response) {
            debugger;
            // user is now logged out
        });
    }
</script>
Jose Rojas
  • 3,490
  • 3
  • 26
  • 40
Pankaj
  • 9,749
  • 32
  • 139
  • 283

2 Answers2

2

It seems that the access token is not persistent when change of page, try store the session in a cookie as said CBroe in a comment, something like this:

<script>
  window.fbAsyncInit = function() {
    FB.init({
        appId      : '{!! env("FACEBOOK_APP_ID") !!}',
        xfbml      : true,
        status     : true,
        cookie     : true,
        version    : '{!! env("Facebook_Version") !!}'
    });
    FB.AppEvents.logPageView();
  };
  (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'));
</script>
Jose Rojas
  • 3,490
  • 3
  • 26
  • 40
0

Use FB.getLoginStatus to obtain the active access token maintained by Facebook JS SDK. And use the response to call FB.logout

function fbLogoutUser() {
    FB.getLoginStatus(function(response) {
        if (response && response.status === 'connected') {
            FB.logout(function(response) {
                // needed for older versions of SDK
                FB.Auth.setAuthResponse(null, 'unknown');
                // reload to remove accessToken from browser cookies
                document.location.reload();
            });
        } else{
            // You are not logged in
        }
    });
}

Reloading the page after logout will remove current session's access token from the browser cookies.

Newton Joshua
  • 745
  • 1
  • 10
  • 21
  • In your original code I could see `FB.init`, `FB.AppEvents` and `FB.logout`, If FB is not undefined at that instance, it shouldn't be undefined here too. Make sure you call it after the FaceBook JS SDK is initialized and FB is available. – Newton Joshua Jul 28 '17 at 05:52
  • This is the original problem. I am getting exception: FB.logout() called without an access token. – Pankaj Jul 31 '17 at 01:01