Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

facebook API how is onlogin called

Can any one tell me how exactly the FB API works. It looks to be a basic question but I am really confused.

Question: I have onlogin(). When I click the login button, I am expecting it to call this function. But in the code I pasted: I see that alert-test is printed first and than the FB.api is called.

So, it looks like onlogin is called first, then the FB API ...is there a way I can call this function only once.

<body>
        <div id="fb-root"></div>
<script>

      window.fbAsyncInit = function() {
          FB.init({appId: 'XXX', status: true, cookie: true, xfbml: true});

      };
      (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol +
          '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
      }());

    function checkFacebookLogin() {
    FB.api('/me', function(response) {
        alert("Name: "+ response.name + "\nFirst name: "+ response.first_name + "ID: "+response.id);
    });

    alert('test');

    }

    </script>


 <p id="fb_login_button_1"><fb:login-button  onlogin="checkFacebookLogin();"  size="medium" scope="user_about_me">Sign in using Facebook</fb:login-button></p>


</body>v

My main issue is the function should be called only once....but it is getting called twice.

like image 986
The Learner Avatar asked Aug 02 '12 05:08

The Learner


1 Answers

    <body>
        <div id="fb-root"></div>
<script>

      window.fbAsyncInit = function() {
          FB.init({appId: 'XXX', status: true, cookie: true, xfbml: true});

      };
      (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol +
          '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
      }());

    function fetchUserDetail()
    {
        FB.api('/me', function(response) {
                alert("Name: "+ response.name + "\nFirst name: "+ response.first_name + "ID: "+response.id);
            });
    }

    function checkFacebookLogin() 
    {
        FB.getLoginStatus(function(response) {
          if (response.status === 'connected') {
            fetchUserDetail();
          } 
          else 
          {
            initiateFBLogin();
          }
         });
    }

    function initiateFBLogin()
    {
        FB.login(function(response) {
           fetchUserDetail();
         });
    }
    </script>


 <input type="button" value="Sign in using Facebook" onclick="checkFacebookLogin();"/>


</body>
like image 84
Purusottam Kaushik Avatar answered Sep 27 '22 17:09

Purusottam Kaushik