search

Create Facebook Login in Javascript

Comments:
Likes:
Shares:
Best Division

Best Division  @bestdivision

Published On - Last Updated -
function checkLoginState() {               // Called when a person is finished with the Login Button.
    FB.getLoginStatus(function (response) {   // See the onlogin handler
        statusChangeCallback(response);
    });
}

function statusChangeCallback(response) {  // Called with the results from FB.getLoginStatus().
    console.log('statusChangeCallback');
    console.log(response);                   // The current login status of the person.
    if (response.status === 'connected') {   // Logged into your webpage and Facebook.
        saveFBLoginDetails();
    } else {

    }
}

$('#facebook-login-button').on('click', function () {
    FB.login(function (response) {
        if (response.status === 'connected') {
            saveFBLoginDetails();
            console.log("Ho gaya ");
        } else {
            // Not logged into your webpage or we are unable to tell.
            console.log("Not logged into facebook");

        }
    });
});

// Testing Graph API after login.  See statusChangeCallback() for when this call is made.
function saveFBLoginDetails() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function (response) {
        console.log(response);
        debugger;
        console.log('Successful login for: ' + response.name);
        document.getElementById('status').innerHTML =
            'Thanks for logging in, ' + response.name + '!';

        //Save fb login details to database
        var formData = {
            'id': response.id,
            'full_name': response.name
        };

        $.ajax({
            type: 'POST',
            url: globalBaseUrl + 'auth/save_fb_login_details',
            data: formData,
            success: function (data) {
                console.log(data);
            },
            error: function (data) {
                console.log("error");
                console.log(data);
            }
        });

    });
}

$('document').ready(function () {
    //  1st number
    window.fbAsyncInit = function () {
        FB.init({
            appId: '<?php echo $app_id; ?>',
            cookie: true,                     // Enable cookies to allow the server to access the session.
            xfbml: true,                     // Parse social plugins on this webpage.
            version: '<?php echo $api_version; ?>'           // Use this Graph API version for this call.
        });

        // 3rd number
        FB.getLoginStatus(function (response) {   // Called after the JS SDK has been initialized.
            statusChangeCallback(response);        // Returns the login status.
        });
    };

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

});
Similar Blogs
0 Commentssort Sort By

@abcd 1 days ago

Aquí los que apoyamos a Los del limit desde sus inicios..

dislike
reply
sdfsdf