Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Logout from my website in Laravel

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>
like image 762
Pankaj Avatar asked Jul 03 '17 15:07

Pankaj


1 Answers

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>
like image 50
Jose Rojas Avatar answered Oct 24 '22 06:10

Jose Rojas