googleads
Hybridauth for User Authentication With Laravel Development
preloder
Standard

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.

 

In controller, include the below classes:

 

use \Hybrid_Auth as Hybridauth;

use \Hybrid_Endpoint as HybridEndpoint;

To connect social media from controller:

 

In callback function:

 

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

 

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

 

 

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

 

The following two tabs change content below.

Manikandan Thangaraj

A Passionate full stack developer with 9 years of experience in full stack web application development and expertise in technologies like PHP and Angular. He has huge craze in learning new things about technologies and constantly shares his knowledge with others.

Leave a Reply

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

[contact-form-7 id="120788" title="Web Page Form"]

Schedule Your Call