This is my Facebook connect Code:
<script>
jQuery(document).ready(function($){
$(".fb-login-button").live('click', function() {
console.log('click');
fbEnsureInit(function() {
console.log('EnsureInit');
FB.login(function(response) {
console.log('FB.login');
// redirect will be done
}, {scope: 'email'});
});
});
});
window.fbAsyncInit = function() {
FB.init({
appId : '105224616211361', // App ID
/*channelUrl : '///channel.html', // Channel File // We don't do not to get caching problems with the laguage!*/
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
oauth : true, // enable OAuth 2.0
xfbml : true // parse XFBML
});
// Additional initialization code here
fbApiInitialized = true;
// whenever the user logs in, we refresh the page
FB.Event.subscribe('auth.login', function(response) {
try {
if (response.status == 'connected') {
console.log('connected');
$.ajax({
type: "POST",
url: "/?eID=login&modus=facebook&fb-login=1",
data: $(this).serialize(),
success: function(msg) {
console.log("LOGIN");
window.location.reload();
}
})
} else {
console.log('not connected');
window.location.reload();
}
} catch(err) {
window.location.reload();
}
});
};
/**
* Make sure that the FB is initialized!
*/
function fbEnsureInit(callback) {
if (!window.fbApiInitialized) {
setTimeout(function() { fbEnsureInit(callback); }, 50);
} else {
if (callback) { callback(); }
}
}
/**
* Load the JS SDK directly from Facebook
*/
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/de_DE/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
The SDK gets loaded. I can see the FB- Button and when i click it the Code works until
console.log('FB.login');
The FB-Popup pops up and nothing happens… This should be the point where:
FB.Event.subscribe('auth.login', function(response) {
should get called… but it doesn’t.. can anyone help me with this problem?
Thanks.
EDIT:
I just noticed that this only happens, when I am already logged in into Facebook. If I am not logged in, the Login Popup pops up twice(!) and I can login to Facebook and the function FB.Event.Subscribe gets called.
So how can I see if the user is logged in to Facebook and is connected to my application?
And why is the Popup called twice?
You need to use
FB.getLoginStatusif you want to check whether the user is connected to your application.The login popup is fired twice because the FB-button has a login function too. Remove your ajax call to
$(".fb-login-button").liveand modify your login button like this: