Hybridauth for User Authentication With Laravel Development

In this article, we will see how to connect the platforms Twitter and Google for user authentication, in the Laravel Development environment.

 

In a modern website, most people prefer to sign up/sign in via social media like Facebook, Twitter, Gmail, etc. For this purpose, in Laravel Development, we have many custom plugins pertaining to each social media. But here we are going to use HybirdAuth (OAuth 2) plugin. In our previous blog I had provided a step-by-step process that explains how to connect Facebook API with Laravel for user authentication.

Here, we are going to discuss how to connect Twitter and Google APIs in Laravel Development environment using HybirdAuth.

 

To get Twitter API keys:

 

1. Go to https://dev.twitter.com/apps and create a new application by clicking “Create New App”.
2. Fill out application name, description, website and callback URL.
3. Put the website address in which you re going to use Twitter.
4.Enter the callback url: “http://www.yourwebsite.com/path_to_hybridauth/?hauth.done=Twitter”

Once you have registered, copy and paste the created application credentials (App ID and Secret) into the HybridAuth config file.

 

To get Google API keys:

 

1. Go to https://code.google.com/apis/console/ and create a new project.
2. Go to API Access under API Project. After that click on Create an OAuth 2.0 client ID to create a new application.
3. A pop-up named “Create Client ID” will appear. Fill out any required fields such as the application name and description. Click on Next. On the popup, set Application type to Web application and switch to advanced settings by clicking on ‘more options’.
4. Provide this URL as the Callback URL for your application: http://www.yourwebsite.com/path_to_hybridauth/?hauth.done=Google

Once you have registered, copy and paste the created application credentials (Client ID and Secret) into the HybridAuth config file.

 

We are going to use HybirdAuth PHP plugin and API Keys to connect to Twitter and Google oAuth API.

 

The goal of HybridAuth is to act as an abstract API between your application and various social APIs and identify providers such as Facebook, Twitter and Google.

 

To Install HybirdAuth:

 

composer require hybridauth/hybridauth

 

This command will automatically download the package and class namespace will added.

 

“composer” is a dependency manager for PHP. Using composer we can install PHP framework plugins. To more composer refer : https://getcomposer.org/

 

Configuration for Connect:

 

We can create a new configuration file inside the config folder.


config/hybirdauth.php

<?php
return [
$authconfig => [
"base_url" => "http://www.yourwebsite.com/path/to/hybridauth/",
"providers" => [
"Twitter" => [
"enabled" => true,
"keys" => array ( "key" => "", "secret" => "" )
],
"Google" => [
"enabled" => true,
"keys" => array ( "id" => "PUT_YOURS_HERE", "secret" => "PUT_YOURS_HERE" ),
"scope" => "https://www.googleapis.com/auth/userinfo.profile ". // optional
"https://www.googleapis.com/auth/userinfo.email" , // optional
"access_type" => "offline", // optional
"approval_prompt" => "force", // optional
"hd" => "domain.com" // optional
] ] ] ] ?>

In controller, include the below classes:

 

use \Hybrid_Auth as Hybridauth;

use \Hybrid_Endpoint as HybridEndpoint;

To connect social media from controller:

 


$config = Config::get('hybirdauth.authconfig');

$hybridAuth = new Hybridauth($config);
$provider = $hybridAuth->authenticate([Social_Media_Name]); // Social_Media_Name - Twitter or Google.
$profile = $provider->getUserProfile();

 

In callback function:


HybridEndpoint::process();

return;

Following is the exact snippet of code that provides the process of connecting the respective APIs for Twitter and Google:

 

In root/app/Http/routes.php


Route::get(‘socialAuth/{type}’, array("as" => "socialAuth", 'uses'=>'SocialConnectController@getSocialLogin'));

Route::get('callback', array("as" => "callback", 'uses'=>'SocialConnectController@getSocialProcess'));

Route::get(‘logout’, array("as" => ‘logout’, 'uses'=>'SocialConnectController@getLogout'));

In root/app/Http/Controllers/SocialConnectController.php


use \Hybrid_Auth as Hybridauth;
use \Hybrid_Endpoint as HybridEndpoint;

class SocialConnectController extends \BaseController {

public function getSocialLogin($type) // $type = Twitter or Google
{
try
{
$config = Config::get('hybirdauth.authconfig');
$hybridAuth = new Hybridauth($config);
$provider = $hybridAuth->authenticate($type);
$profile = $provider->getUserProfile();
}
catch(Exception $e)
{
return Redirect::route('user')->with(‘type’, $type);
}
return var_dump($profile).'<br><a href="logout">Logout</a>';
}
public function getCallBack($auth=NULL)
{
HybridEndpoint::process();
return;
}
public function getLogout()
{
Hybrid_Provider_Adapter::logout(); // Will clear the session and logout user from social media.
// Use below code to logout from Laravel session

//$this->auth->logout();
//Session::flush();
return redirect('/');
}
}
- $profile

 

 

From $profile you can get identifier, displayName, description, firstName, lastName, photoURL, profileURL, webSiteURL etc.

 

To get:

 

$profile->identifier

 

In browser:

 

http://www.yourwebsite.com/socialAuth/TYPE

 

where TYPE = Twitter or Google

This is how we connect social platforms like Twitter and Google in the Laravel Development environment.

References:

 

Hybrid Document: http://hybridauth.sourceforge.net/userguide/IDProvider_info_Twitter.html, http://hybridauth.sourceforge.net/userguide/IDProvider_info_Google.html