The problem is that FB.Event
is not subscribed since FB
was not initialized when you tried to subscribe this event. See console for the error, you'll see:
FB is not defined
The reason is that the FB object is defined asynchronously under window.fbAsyncInit
, so whenever you want to do a API call or use the FB
it must be done after the FB
is defined.
So the solution to your problem is that, write the FB.event
after the FB
is defined (not before it is defined), just like this-
window.fbAsyncInit = function() {
FB.init({
appId : '{{app_id}}',
status : true,
xfbml : true
});
// write all your "FB" code here, since the FB is defined now
FB.Event.subscribe('auth.authResponseChange', function(response) {
if (response.status === 'connected') {
onLogin(response);
} else if (response.status === 'not_authorized') {
FB.login();
} else {
FB.login();
}
});
};