How to Login with Facebook in CodeIgniter

Integrate Facebook login is the most used feature for today’s web application. Login with Facebook feature helps users to log into the web application easily. In this tutorial, we’re going to explain how to integrate Facebook login in CodeIgniter using Facebook PHP SDK with Facebook Graph API.

Before you begin to integrate Login with Facebook in CodeIgniter, check below steps

Click here to download and Click here to demo

Step 1: Config (application/config/autoload.php)

$autoload['libraries'] = array('database','email','session');
$autoload['helper'] = array('url', 'file');

Step 2: Config (application/config/facebook.php)

$config['facebook_app_id']              = '';
$config['facebook_app_secret']          = '';
$config['facebook_login_type']          = 'web';
$config['facebook_login_redirect_url']  = 'login';
$config['facebook_logout_redirect_url'] = 'login/logout';
$config['facebook_permissions']         = array('email');
$config['facebook_graph_version']       = 'v2.6';
$config['facebook_auth_on_load']        = TRUE;

Step 3: application/libraries/Facebook.php

Facebook App and API configuration variables are defined in this file.

Step 4: Load Facebook libraries in __construct function

function __construct() {
	parent::__construct();
	$this->load->library('facebook');
}

Step 5: Facebook Account button on click and redirect controller function like (index())

public function index(){
	$userData = array();
	if($this->facebook->is_authenticated()){
		$userProfile = $this->facebook->request('get', '/me?fields=id,first_name,last_name,email,gender,locale,picture');
		echo "<pre>";
		print_r($userProfile);
		die;
	}
	else
	{
		$data['authUrl'] =  $this->facebook->login_url();
	}
	$this->load->view('login',$data);
}

Step 6: Facebook Account Response Output

Array
(
    [id] => 1455845184510060dsfsd
    [first_name] => Hardik
    [last_name] => Dayani
    [email] => example@ymail.com
    [gender] => male
    [locale] => en_GB
    [picture] => Array
        (
            [data] => Array
                (
                    [is_silhouette] => 
                    [url] => https://scontent.xx.fbcdn.net/v/t1.0-1/c0.0.50.50/p50x50
                )

        )

)

Step 7: Logout Controller Function

public function logout() {
	$this->facebook->destroy_session();
	redirect('/login');
}

Leave a Reply

Your email address will not be published. Required fields are marked *