@mallory_cormier
To login with Facebook in CodeIgniter, you can follow these steps:
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.