Search code examples
javascriptfacebook

Facebook Connect Using JavaScript


to connect to facebook, i've placed this in body tag:

<body>
      <div id="fb-root"></div>
      <script>
        window.fbAsyncInit = function() {
          FB.init({
            appId      : 'xxxxxxx',
            status     : true, 
            cookie     : true,
            xfbml      : true,
            oauth      : true,
          });
        };
        (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">Login with Facebook</div>
    </body>

The only problem i'm having is how to know that the user has login or not??


Solution

  • function login() {
    
            FB.login(function(response) {
                if (response.authResponse) {
                    alert('Success!');
                }else{
                    alert('Login Failed!');
                }
            }, {scope: 'email'});
         }