0

Hi I'm trying to retrieve some data from reponse of Facebook login but I didn't succeed to return these values this is the code I made:

function loginfb(){
    var resp=[];

    FB.login(function(response,resp) {

        //        console('response:' + response.status);
        if (response.status === 'connected'){
            swal({
                title:'Connecté',
                text: "Vous êtes connecté avec votre compte Facebook",
                type: "success",
                confirmButtonColor: "#81d8d0"
            });                   

            getName();
            resp = [response.status,response.email,response.name];

        }else{
            swal("Erreur", "Nous n'avons pas pu vous conecter à votre compte facebook, désolé", "error");
            resp = [response.status];
        }
    });

    console.log('statusssss: ' + resp[1]);
}


window.fbAsyncInit = function initfb() {
    FB.init({
        appId      : 'XXXXXXXXXXXXXX',
        cookie     : true,  // enable cookies to allow the server to access 
                            // the session
        xfbml      : true,  // parse social plugins on this page
        version    : 'v2.3' // use version 2.2
    });
}; 

// Load the SDK asynchronously
(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'));

// Here we run a very simple test of the Graph API after login is
// successful.  See statusChangeCallback() for when this call is made.
function getName() {
    //    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
        document.getElementById('status').innerHTML =       'Bienvenue, ' +        response.name;
    });
}

The login phase is ok the user is connected but I cannot return values of response to handle it later on the code. Please, help.

jasonscript
  • 5,189
  • 1
  • 23
  • 39
sad ill
  • 11
  • 4
  • It's because the response from the `FB.login` method is **asynchronous** Your `console.log('status')` method is being called before the `login` is complete – jasonscript Jul 10 '15 at 01:33
  • possible duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – CBroe Jul 10 '15 at 01:33
  • Ok thanks the goal ws to push the value after on cookies finally i did it directly from FB.API call – sad ill Jul 10 '15 at 08:45

1 Answers1

0

Finally this is the code I did and it works fine for me:

function loginfb(){


FB.login(function(response) {


//        console('response:' + response.status);
if (response.status === 'connected'){

       FB.api('/me', function(response) {

  swal({
        title:'Connecté',
        text: "Vous êtes connecté avec votre compte Facebook",
        type: "success",
        confirmButtonColor: "#81d8d0"
            },function(){


  document.cookie="userCookie=99999999;path=/";       
  document.cookie="userName="+ response.name + ";path=/";           
  document.getElementById('status').innerHTML =       'Bienvenue, ' + response.name; 
  location.reload();

   });  



  });

  }else{
    swal("Erreur", "Nous n'avons pas pu vous conecter à votre compte facebook, désolé", "error");

  }


  });





}


window.fbAsyncInit = function initfb() {
FB.init({
appId      : 'XXXXXXXXXXXXXXXXXX',
cookie     : true,  // enable cookies to allow the server to access 
                    // the session
xfbml      : true,  // parse social plugins on this page
version    : 'v2.3' // use version 2.2
 });


 };

 // Load the SDK asynchronously
(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'));
sad ill
  • 11
  • 4