Search code examples
javascripthtmlfacebook

FB.Event.subscribe('auth.login') won't trigger everytime?


<div id="fb-root"></div>
    <script>
        window.fbAsyncInit = function() {
            FB.init({
                appId      : 'MY_APP_ID',
                status     : true,
                cookie     : true,
                xfbml      : true
            });

            FB.Event.subscribe('auth.login', function(response) {
               $.ajax({
                    url: "http://example.com/fbconnect",
                    success: function(){
                     window.location.reload();
        }
                 });
            });
        };
        (function(d){
            var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
            js = d.createElement('script'); js.id = id; js.async = true;
            js.src = "//connect.facebook.net/en_US/all.js";
            d.getElementsByTagName('head')[0].appendChild(js);
        }(document));
    </script>


<div class="fb-login-button" data-perms="email,user_birthday,user_hometown,user_location">Login with Facebook</div>

It's working with only two cases:

  1. when a user is NOT logged in and log in to facebook it triggers.
  2. when a user is logged in but didn't authorize the app yet it triggers.

the third scenario which is not working is when a user is logged in and He is already authorized the app the FB.Event.subscribe('auth.login') won't trigger !!!


Solution

  • I encountered the same thing as you and I found a function, "getLoginStatus", that might be of interest for you.

    When the user is already logged in and has granted your app all the permissions, then there is no need to log in again and therefore the event won't be triggered.

    However, if the response of the "getLoginStatus" is "connected" you can do the stuff you want to do when the user is already logged in and has granted your app all the permissions.

    The complete script might look something like this:

    <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId      : 'APPID', // App ID
          channelUrl : 'CHANNEL_URL', // Channel File
          status     : true, // check login status
          cookie     : true, // enable cookies to allow the server to access the session
          xfbml      : true  // parse XFBML
        });
    
        FB.getLoginStatus(function(response) {
          if (response.status === 'connected') {
            // the user is logged in and connected to your
            // app, and response.authResponse supplies
            // the user's ID, a valid access token, a signed
            // request, and the time the access token 
            // and signed request each expire
            var uid = response.authResponse.userID;
            var accessToken = response.authResponse.accessToken;
            FB.api('/me', function(response) {
              if (response.name != undefined ) {
                console.log("Welcome, " + response.name);
              }         
            });
          } else if (response.status === 'not_authorized') {
            // the user is logged in to Facebook, 
            // but not connected to the app
          } else {
            // the user isn't even logged in to Facebook.
          }
        });        
    
        // Additional initialization code here
    
      };
    
      // Load the SDK Asynchronously
      (function(d){
         var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
         js = d.createElement('script'); js.id = id; js.async = true;
         js.src = "//connect.facebook.net/en_US/all.js";
         d.getElementsByTagName('head')[0].appendChild(js);
       }(document));
    </script>