Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Integrate Social Sign On in your PHP apps using HybridAuth

DZone's Guide to

Integrate Social Sign On in your PHP apps using HybridAuth

· Java Zone ·
Free Resource

Bring content to any platform with the open-source BloomReach CMS. Try for free.

 

nt.social.network.big_

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.

{
    "require": {
        "hybridauth/hybridauth": "3.0.0.*@dev"
    }
}

Now run the following command to install hybridauth.

composer install

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.
<?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>";

And here is the code of hybrid.php.

<?php
//hybrid.php
require_once __DIR__ . '/vendor/autoload.php';
$endPoint = new \Hybridauth\Endpoint();
$endPoint->process();

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.

<?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>";

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.

BloomReach CMS: the API-first CMS of the future. Open-source & enterprise-grade. - As a Java developer, you will feel at home using Maven builds and your favorite IDE (e.g. Eclipse or IntelliJ) and continuous integration server (e.g. Jenkins). Manage your Java objects using Spring Framework, write your templates in JSP or Freemarker. Try for free.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}