I am currently in the process of integrating a Facebook login button into my website and have made progress, but I have encountered a problem. The Facebook SDK JavaScript code that I am using is as follows:
function statusChangeCallback(response) {
console.log(response);
if (response.status === 'connected') {
getUser();
} else {
document.getElementById('status').innerHTML = 'Please log ' +
'into this app.';
}
}
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
window.fbAsyncInit = function() {
FB.init({
appId : 'XXXXX',
cookie : true,
status : true,
xfbml : true,
version : 'v2.8'
});
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
};
(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 = "https://connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
function getUser() {
FB.api('/me?fields=email,name,birthday,gender', function(response) {
var ufd=JSON.stringify(response);
$.ajax({
type: "POST",
url: "/fb-callback.php",
data: {ud:ufd},
success: function(msg) {
if(msg!=""){
alert("Successfully Logged In !"+msg);
}
else{
alert("Some Error");
}
}
});
});
}
In the getUser() function, the ajax call seems to be triggered twice or the function itself is being called twice. Despite searching for solutions, I have not found anything relevant to address this particular issue.