Let us first understand why I had to write this guide?
- HybridAuth documentation is not good at all.
- There is no updates on codeigniter liberary for hybridAuth 3 yet (Dec 2019).
- I could not find any resources online to get it done.
Let’s start now.
Step1#
On HybridAuth portal it is mentioned to download the latest release from the link below:
Manual Installation
In case you can’t/don’t want to use Composer, or if you never heard of it, you can still include Hybridauth in you application the traditional way by downloading the library archive and unzipping it into your project’s folder.
The required steps are typically the following:
- Download the latest available release at https://github.com/hybridauth/hybridauth/releases [Do Not Download]
- Next, simply unzip the archive file to your project’s directory.
Because this does not have an autoload file in it.
So better you should download the master zip from this link:
https://github.com/hybridauth/hybridauth/archive/master.zip
Step 2#
Extract the content on master.zip into your library folder
/application/liberaries/hybridauth
Now you should see a file autoload inside src folder:
/application/liberaries/hybridauth/src/autoload.php
Step 3#
Include hybridauth into your controller. Make sure you include and use namespace on top of anything in your controller.
Or
It will simple not
// Your controller
<?php
//Include Hybridauth’s basic autoloader
include APPPATH.’/libraries/hybridauth/src/autoload.php’;
//Import Hybridauth’s namespace
use Hybridauth\Hybridauth;
//And so on
Step 4#
Once you included it and load your controller method in the browser. You should see no errors.
If you see errors then you have done something wrong.
But if not then it should show no problems.
Lets put things together and create a hybridauth instance
$config = [
‘callback’ => ‘https://blognow.org/auth/login',
//Providers specifics
‘providers’ => [
‘GitHub’ => [‘enabled’ => true,
‘keys’ => [ ‘id’ => ‘cb9a84bdea0b1786f99d’,
‘secret’ => ‘c1cf446e0ab4f9f7bd2ea190c82e3e03edb171d7’]]
]
];
try{
//Feed configuration array to Hybridauth
$hybridauth = new Hybridauth($config);
//Then we can proceed and sign in with Twitter as an example. If you want to use a diffirent provider,
//simply replace ‘Twitter’ with ‘Google’ or ‘Facebook’.
//Attempt to authenticate users with a provider by name
$adapter = $hybridauth->authenticate(‘GitHub’);
//Returns a boolean of whether the user is connected with Twitter
$isConnected = $adapter->isConnected();
//Retrieve the user’s profile
$userProfile = $adapter->getUserProfile();
//Inspect profile’s public attributes
var_dump($userProfile);
//Disconnect the adapter
$adapter->disconnect();
}
catch(\Exception $e){
echo ‘Oops, we ran into an issue! ‘ . $e->getMessage();
}
That’s it you are able to use hybridauth 3 with codeigniter 3
Any questions leave below in comments