How to login with facebook in codeigniter?

by mallory_cormier , in category: PHP Frameworks , a year ago

How to login with facebook in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by aubrey , a year ago

@mallory_cormier 

To login with Facebook in CodeIgniter, you can follow these steps:

  1. Create a new Facebook App: Go to developers.facebook.com and create a new app. Update the App Domain and Redirect URI in the app settings.
  2. Install Facebook PHP SDK: Download the latest version of Facebook PHP SDK from the GitHub repository. Extract the files and include the autoload.php file in your CodeIgniter project.
  3. Set up Facebook login button: Add a Facebook login button on your website and set the redirect URL to your CodeIgniter controller method.
  4. Create a controller in CodeIgniter: Create a new controller in CodeIgniter and add the following code to handle the Facebook login process:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

use FacebookFacebook;

class FacebookLogin extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        require_once APPPATH.'third_party/facebook_sdk/autoload.php'; //Include Facebook SDK
        $this->load->library('session'); //Load CI session library
    }

    public function login()
    {
        $fb = new Facebook([
            'app_id' => 'YOUR_APP_ID',
            'app_secret' => 'YOUR_APP_SECRET',
            'default_graph_version' => 'v2.10',
        ]);

        $helper = $fb->getRedirectLoginHelper();

        $permissions = ['email']; // Optional permissions

        $loginUrl = $helper->getLoginUrl('http://yourwebsite.com/facebook/callback', $permissions);

        redirect($loginUrl);
    }

    public function callback()
    {
        $fb = new Facebook([
            'app_id' => 'YOUR_APP_ID',
            'app_secret' => 'YOUR_APP_SECRET',
            'default_graph_version' => 'v2.10',
        ]);

        $helper = $fb->getRedirectLoginHelper();

        try {
            $accessToken = $helper->getAccessToken();
        } catch(FacebookExceptionsFacebookResponseException $e) {
            // When Graph returns an error
            echo 'Graph returned an error: ' . $e->getMessage();
            exit;
        } catch(FacebookExceptionsFacebookSDKException $e) {
            // When validation fails or other local issues
            echo 'Facebook SDK returned an error: ' . $e->getMessage();
            exit;
        }

        if (! isset($accessToken)) {
            if ($helper->getError()) {
                header('HTTP/1.0 401 Unauthorized');
                echo "Error: " . $helper->getError() . "
";
                echo "Error Code: " . $helper->getErrorCode() . "
";
                echo "Error Reason: " . $helper->getErrorReason() . "
";
                echo "Error Description: " . $helper->getErrorDescription() . "
";
            } else {
                header('HTTP/1.0 400 Bad Request');
                echo 'Bad request';
            }
            exit;
        }

        // Logged in
        $oAuth2Client = $fb->getOAuth2Client();
        $tokenMetadata = $oAuth2Client->debugToken($accessToken);
        $tokenMetadata->validateAppId('YOUR_APP_ID');

        $longLivedAccessToken = $accessToken->getValue();

        $this->session->set_userdata('access_token', $longLivedAccessToken);

        redirect('your_dashboard_url');
    }
}


This is a basic example of how to handle Facebook login in CodeIgniter. You can customize the code as needed for your application. Remember to replace ‘YOUR_APP_ID’, ‘YOUR_APP_SECRET’ and other placeholders with your actual Facebook app credentials.


That's it! You have now implemented Facebook login in CodeIgniter.

Related Threads:

How to set session in Codeigniter after login?
How to check is user login or not in codeigniter?
How to redirect after google login using codeigniter?
How to create login page using codeigniter?
How to prevent user to "go back" after login in codeigniter?
How to open facebook page inside iframe?