Integrating social sign-ons in a web application can become a tedious task because you need to take care of different endpoints, credentials and finally manage the oauth dance to get the access token. However, using HybridAuth package, this task can be easy as pie 🙂 Let’s have a look
In this example I will show you how to connect to Facebook and Twitter using HybridAuth. So you need to create two applications in Facebook and Twitter each. Save those app’s id and secret somewhere because we will need that in a minute.
Step 1: Install the Package via Composer
Composer is an excellent package manager for PHP apps. Let’s use that to install HybridAuth in our current projects scope. Add a composer.json file in your project path, or update it with the following contents if it already exists. But before that, make sure that you have composer installed in this machine.
[sourcecode language=”javascript”]
{
"require": {
"hybridauth/hybridauth": "3.0.0.*@dev"
}
}
[/sourcecode]
now run the following command to install hybridauth
[sourcecode language=”shell”]
composer install
[/sourcecode]
Step 2: Connect with Facebook
Let’s make a good use of this HybridAuth. This time we need to create two files, fb.php and hybrid.php. Make sure that your facebook app’s callback url points to this hybrid.php. FOllowing is the code of fb.php
[sourcecode language=”php”]
<?php
//fb.php
require_once __DIR__ . ‘/vendor/autoload.php’;
$config = array(
"base_url" => "http://path/to/your/hybrid.php",
"providers" => array(
"Facebook" => array(
"enabled" => true,
"keys" => array("id" => "FB_APP_ID", "secret" => "FB_APP_SECRET"),
"scope" => "email"
)
)
);
$hybridAuth = new \Hybridauth\Hybridauth($config);
$adapter = $hybridAuth->authenticate("Facebook");
$userProfile = $adapter->getUserProfile();
echo "<pre>";
print_r($adapter->getTokens());
print_r($userProfile);
echo "</pre>";
[/sourcecode]
And here is the code of hybrid.php
[sourcecode language=”php”]
<?php
//hybrid.php
require_once __DIR__ . ‘/vendor/autoload.php’;
$endPoint = new \Hybridauth\Endpoint();
$endPoint->process();
[/sourcecode]
Don’t forget to replace the FB_APP_ID and FB_APP_SECRET in fb.php. Now as soon as you point your browser to this fb.php, you will see the FB auth dialog box and once you’re authenticated you will see the user details along with access token 🙂
Step 3: Connect with Twitter
Now, connecting with twitter is a matter of a minute. Just make sure that your twitter application’s callback URL is set to the hybrid.php and write the following code in a new file named twitter.php
[sourcecode language=”php”]
<?php
//twitter.php
require_once __DIR__ . ‘/vendor/autoload.php’;
$config = array(
"base_url" => "http://path/to/your/hybrid.php",
"providers" => array(
"Twitter" => array(
"enabled" => true,
"keys" => array("key" => "TWITTER_APP_ID", "secret" => "TWITTER_APP_SECRET"),
)
)
);
$hybridAuth = new \Hybridauth\Hybridauth($config);
$adapter = $hybridAuth->authenticate("Twitter");
$userProfile = $adapter->getUserProfile();
echo "<pre>";
print_r($adapter->getTokens());
print_r($userProfile);
echo "</pre>";
[/sourcecode]
As soon as you visit this twitter.php, you will be prompted for the authentication. So, integrating social networks with HybridAuth is really easy. I hope you’ve enjoyed this article and I will appreciate your comments 🙂