Integrating social login features into the website is a trend nowadays. This feature makes the 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, and 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 a specific social site), and email. The web application can ask for more details about the user. But, the user has to give access to extra information. It totally depends on user either give access to 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.
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 its own library and its own way to integrate social sign-in features.
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. The HybridAuth team makes our developer’s life easy.
Using this library, we don’t need to read about social site documentation on how to integrate social login into the website. What we all need to do is – install and configure the 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, the use of a Composer is recommended. If you don’t have Composer installed on your system, you can get it from their official website.
Create a composer.json file in your project root directory and place the below code in it.
{
"require" : {
"hybridauth/hybridauth" : "*"
}
}
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 in frustration.
Copy the 2 files config.php and index.php from vendor\hybridauth\hybridauth\hybridauth directory and place them 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 the application details of 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 the 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 to write an actual social login code.
Let’s say we need to use social login with Facebook then our code will be 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 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.
In the next line, we fetch the details of an authenticated user using the 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 a script social login in PHP using the 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.