In the last tutorial, we have learnt how to perform different processing by checking whether the user is connected or not when the web page is loaded.

Now, how about if the connect status or session is changed after the page is loaded? Just in case you don't understand why the connect status can be changed after the page is loaded, let's consider the following case:

To handle this kind of scenarios, the JS SDK has exposed various events that we can subscribe to by providing a callback function. Once subscribed, whenever a subscribed event is fired, the corresponding callback function will be executed.


Subscribing to the Authentication Events

There are 4 authentication related events, with their nature (i.e. when the event will be fired) being self-explanatory as how they are named.

To subscribe to any of the events, the FB.Event.subscribe method is to be used. For example, the following code shows how to subscribe to the auth.login with the callback function defined.

 var onAuthLogin = function(response) {
   // do something when the user log in
}; FB.Event.subscribe('auth.login', onAuthLogin);

It is obvious (though not necessary) that you can perform the subscription right after calling the FB.init method.

And when you don't want to get notified anymore on the event, simply use the FB.Event.unsubscribe method.