FB documentation says:
As noted in the reference docs for this function, it results in a popup window showing the Login dialog, and therefore should only be invoked as a result of someone clicking an HTML button (so that the popup isn't blocked by browsers).
And I did as it says, I put the FB.login
function into an onCLick function. But the the Login dialog
is still blocked. Why? How to reorder the code?
// Facebook
// Here is a click event, so FB.login is inside a click function.
$("#login_btn_fb").on("click", function() {
function getUserData(res, fCallback) {
if(res.authResponse != null) {
FB.api('/me', function(response) {
console.log('/me_: ', response);
});
}
else {
console.log("getUserData CANCEL: ", res);
return;
}
};
FB.getLoginStatus(function(res) {
var uid = null;
var accessToken = null;
if($.isPlainObject(res)) {
// Fb+ App+
if(res.status == "connected") {
console.error("connected");
uid = res.authResponse.userID;
accessToken = res.authResponse.accessToken;
getUserData(res, null);
}
// Fb+ App-
else if(res.status == "not_authorized") {
console.error("not_authorized");
FB.login(function(res) {
getUserData(res, null);
}, {scope: 'email,user_birthday,user_photos,public_profile,user_location'});
}
// Fb- App-
else {
console.log("UNKNOWN");
FB.login(function(res) {
// console.log("===UNK FB.login res: ", res);
getUserData(res, null);
}, {scope: 'email,user_birthday,user_photos,public_profile,user_location', return_scopes: true});
};
}
// ERROR with FB
else {
alert("Facebook sign in failure. Please try again later.");
return;
}
});
});
You did not put FB.login in a function that is directly called by a User event, but in an asynchronous callback function of FB.getLoginStatus.
Use FB.getLoginStatus to refresh a User Session and to check if the User is logged in right when you load your Page (right after FB.init), and FB.login only on User interaction - but NOT in an asynchronous callback, of course.