Social Login In PHP Using HybridAuth Library

Integrating social login feature in the website is a trend nowadays. This feature makes user login and registration process easy. If a user is ready to use their social profile on your website then they don’t need to fill up your registration form, activate their account.

Social sites also do not provide private information of a user to the website. Instead, they provide just basic details like name, gender, social id(on specific social site), email. The web application can ask for more details of a user. But, the user has to give access to extra information. It totally depends on user either give access of more information or not. Some social sites like Twitter do not provide an email of a user. The point is, it is safe to use your social site profile for interaction with a website.

Social Login

For a developer, it’s not simple to write a code for social login. First, they need to read the documentation provided by social sites like Facebook, Twitter. Each social site has their own library and own way to integrate social sign in feature.

For instance, you are looking to add a social login with Facebook and Twitter. In that case, a developer needs to write a different code for both social sites as provided by them in their official documentation.

It’s not easy for a developer to develop a code if you have more than one social site to interact with.

Thanks to HybridAuth – open source social sign on PHP library. HybridAuth team makes our developers life easy.

Using this library, we don’t need to read about social sites documentation on how to integrate social login in the website. What we all need to do is – install and configure HybridAuth library correctly and register an application on social sites(which is necessary even if we don’t use HybridAuth Library).

Installation

To install the HybridAuth library, use of composer is recommended. If you don’t have composer installed on your system, you can get it from their offical website.

Create a composer.json file in your project root directory and place the below code in it.

{
    "require" : {
        "hybridauth/hybridauth" : "2.9.6"
    }
}

We have passed “2.9.6” because at the time of writing this article it is the latest stable release of HybridAuth library.

Open the terminal in the project’s root directory and run the command composer install. It will install the HybridAuth library version 2.9.6 on your system.

Configuration

Make sure you are configuring a library correctly. If you are failed to configure it correctly then you will not get a benefit of a library and end up into the frustration.

Copy the 2 files config.php and index.php from vendor\hybridauth\hybridauth\hybridauth directory and place in your root directory of a project. Rename the index.php file to hybridauth.php as we might have another index.php at the same location.

Open the config.php file and add your application keys and secret in the related providers array.

Click here to get the list of all social providers supported by HybridAuth. We also find providers and additional providers in the installed library itself. To see providers go to vendor\hybridauth\hybridauth\hybridauth\Hybrid\Providers and for additional providers check out the directory vendor\hybridauth\hybridauth\additional-providers.

Let’s say we need to specify application details of a Facebook. For this, in the config.php for Facebook provider our code is as follows.

return array(
    "base_url" => "YOUR_SITE_URL/hybridauth.php",
    "providers" => array(
        //other providers code
        "Facebook" => array(
            "enabled" => true,
            "keys" => array("id" => "YOUR_APP_ID", "secret" => "YOUR_APP_SECRET"),
            "trustForwarded" => false,
        ),
        //other providers code
    ),
);

Make sure you have replaced placeholders YOUR_APP_ID and YOUR_APP_SECRET with the actual values.

We have also passed the path of a hybridauth.php file for base_url.

Next, open the hybridauth.php file and make following changes.

Replace

require_once( "Hybrid/Auth.php" );
require_once( "Hybrid/Endpoint.php" );

Hybrid_Endpoint::process();

With

require 'vendor/autoload.php';

Hybrid_Endpoint::process();

Actual Social Login Code Using HybridAuth Library

At this stage, we have completed with installation and configuration of a library. Next, we need write an actual social login code.

Let’s say we need use social login with Facebook then our code will as below. We assume you have sign-in.php in the root folder where you need to write a code.

sign-in.php

require 'vendor/autoload.php';

try {
	$hybridauth = new Hybrid_Auth( 'config.php' );

	$adapter = $hybridauth->authenticate( "Facebook" ); //it can be Twitter, Google etc.

	$user_profile = $adapter->getUserProfile();
} catch(\Exception $e){
    echo 'Oops, we ran into an issue! ' . $e->getMessage();
}

The parameter passed to the authenticate() method must match with the provider key in the config.php file. The function authenticate() checks if a user is authenticated. If the user is not authenticated then it redirects to the login page of a related social site. After successful authentication user again redirects to our sign-in.php file.

On the next line, we fetch the details of a authenticated user using getUserProfile() function.

To log out the user attached to the provider we simply write the below code in our logout.php file.

logout.php

require 'vendor/autoload.php';

Hybrid_Auth::logoutAllProviders();

We hope you understand how to add script social login in PHP using HybridAuth library. If you have any questions or suggestions please leave a comment below. You may also like to read our article Social Login System Using Laravel Socialite.

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *