Showing posts with label fb script. Show all posts
Showing posts with label fb script. Show all posts

Wednesday, 1 October 2014

Easy to integrate Facebook login to your web application

<fb:login-button scope="public_profile,email" onlogin="checkLoginState();">
</fb:login-button>
<span id="status"></span>
<script type='text/javascript'>
function statusChangeCallback(response) {
    console.log('statusChangeCallback');
    console.log(response);
    if (response.status === 'connected') {
      testAPI();
    } else if (response.status === 'not_authorized') {
      document.getElementById('status').innerHTML = 'Please log ' +'into this app.';
    } else {
      document.getElementById('status').innerHTML = 'Please log ' +'into Facebook.';
    }
}
function checkLoginState() {
    FB.getLoginStatus(function(response) {  statusChangeCallback(response); });
}

window.fbAsyncInit = function() {
    FB.init({
        appId      : "FB_APP_ID", 
        cookie     : true,  // enable cookies to allow the server to access // the session
        xfbml      : true,  // parse social plugins on this page
        version    : 'v2.1' // use version 2.1
    });
    //FB.getLoginStatus(function(response) {  statusChangeCallback(response); });
};

(function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

function testAPI() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
        console.log('Successful login for: ' + response.name);
        var name = response.name;
        var email = response.email;
        document.getElementById('status').innerHTML = 'Thanks for logging in, ' + response.email + '!';
    });
}
</script>