Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getLoginStatus javascript facebook disappear when page reload

I implement the facebook connect and it works fine.

This function return my name after login.

FB.getLoginStatus(function (response) {
    if (response && response.status === 'connected') {
         FB.api('/me', function(response) {
            console.log('Good to see you, ' + response.name + '.');
        });
        //console.log('connected');
    } else {
        console.log('not connected with FB');
    }
});  

But, when I reload the page, I recall this function and it returns 'not connected with FB'.

I join my login function to help :

    FB.login(function(response) {

    if (response.authResponse){
      //L'utilisateur a autorisé l'application
      //window.location.replace("http://adresse/de/redirection");
    } else {
      //L'utilisateur n'a pas autorisé l'application
    }
  }, {scope: 'public_profile,user_friends,email'});

I don't find any solution in facebook documentation

To show the issue, my console log is like this: console log with my code

EDIT : Please find attach my full code. I process from localhost

<!DOCTYPE html>
<html>
<head>
<title>Facebook Login JavaScript Example</title>
<meta charset="UTF-8">

</head>
<body>

<button id="loginBtn">Facebook Login</button>
<div id="response"></div>

</body>
<script>

function getUserData() {
    FB.api('/me', function(response) {
        document.getElementById('response').innerHTML = 'Hello ' + response.name;
    });
}

window.fbAsyncInit = function() {
    //SDK loaded, initialize it
    FB.init({
        appId      : 'XXXX',
        xfbml      : true,
        version    : 'v2.2'
    });

    //check user session and refresh it
    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
            //user is authorized
            document.getElementById('loginBtn').style.display = 'none';
            getUserData();
        } else {
            console.log(response.status);
            //user is not authorized
        }
    });
};

//load the JavaScript SDK
(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'));

//add event listener to login button
document.getElementById('loginBtn').addEventListener('click', function() {
    //do the login
    FB.login(function(response) {
        if (response.authResponse) {
            //user just authorized your app
            document.getElementById('loginBtn').style.display = 'none';
            console.log(response);
            getUserData();
        }
    }, {scope: 'public_profile,user_friends,email', return_scopes: true});
}, false); 
</script>
</html>

Thanks for support

like image 742
Thomas Dupont Avatar asked Dec 24 '22 05:12

Thomas Dupont


1 Answers

For anyone reading this page sloppily (as I did), the correct answer was posted as a comment to the question.

The solution is to add cookie: true to the options object in your call to FB.init.

like image 133
korona Avatar answered May 04 '23 00:05

korona