• How To Check Facebook Login and Authentication

    Facebook provides an authentication api which you can use as an alternate for the traditional login system or also to import facebook user profile information to your website. Basically how this works is you create a facebook app, ask user to grant permission to your app to access his profile information, after he has granted access you can create account on your website using his profile information.
    This is the workflow which gets followed when integrating facebook login on your site (helpful for a beginner)

    Create a facebook app for your domain
    Place a login button on your website

    When user clicks on the login button there conditions are possible.

    1. User is Not Logged In: Facebook dialog pop’s up and asks user to login.

    2. User is Logged In, but has not authorized the app: Facebook pop’s up a dialog asking user to give permission to your app.

    Where facebook redirects, we retrieve user information like name, email, facebook id etc and create his account on our website.

    The entire code is divided in two part
    1. javascript
    2. php

    Code:

    <div id="fb-root"></div>
    
    <script type="text/javascript">
    
    //<![CDATA[
    
    window.fbAsyncInit = function() {
    
    FB.init({
    
    appId      : 'XXXXXXXXXXXXXX', // App ID
    
    channelURL : '', // Channel File, not required so leave empty
    
    status     : true, // check login status
    
    cookie     : true, // enable cookies to allow the server to access the session
    
    oauth      : true, // enable OAuth 2.0
    
    xfbml      : false  // parse XFBML
    
    });
    
    };
    
    // logs the user in the application and facebook
    
    function login(){
    
    FB.getLoginStatus(function(r){
    
    if(r.status === 'connected'){
    
    window.location.href = 'fbconnect.php';
    
    }else{
    
    FB.login(function(response) {
    
    if(response.authResponse) {
    
    //if (response.perms)
    
    window.location.href = 'fbconnect.php';
    
    } else {
    
    // user is not logged in
    
    }
    
    },{scope:'email'}); // which data to access from user profile
    
    }
    
    });
    
    }
    
    // Load the SDK Asynchronously
    
    (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);
    
    }());
    
    //]]>
    
    </script>
    									

    Next in the php file i.e fbconnect.php we need to add code to retrieve facebook user profile details. We need facebook php-sdk for this purpose which can be downloaded from https://github.com/facebook/php-sdk

    <?php
    
    require_once 'src/facebook.php'; //include the facebook php sdk
    
    $facebook = new Facebook(array(
    
    'appId'  => 'XXXXXXXXXXXXXX',    //app id
    
    'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX', // app secret
    
    ));
    
    $user = $facebook->getUser();
    
    if ($user) { // check if current user is authenticated
    
    try {
    
    // Proceed knowing you have a logged in user who's authenticated.
    
    $user_profile = $facebook->api('/me');  //get current user's profile information using open graph
    
    }
    
    catch(Exception $e){}
    
    }
    
    ?>
    									

    The varaible $user_profile will contain the profile information of the user like firstname, lastname, email, facebook id etc. You can use this information to create his account on your website.

Comments on this post

Leave a Reply

  • Security Code :


    seven − 3 =