Open with fade-zoom animation

Stay ahead...
Receive latest updates directly in your Inbox!

Adding social media registration and login to laravel application.

Registration & Login using Social Media in Laravel (Socialite Package)

With the growing usage of social media, Social Media Registration and authentication are important for any web applications. The advantage of having them is users can Register/Login into your application with the click of a button and don’t have to remember a new credential for the application.

In this blog, you will look into integrating social media Registration/Login in your Laravel application using Laravel Official package – Socialite.

Socialite package currently supports authentication with Facebook, Twitter, LinkedIn, Google, GitHub, and Bitbucket. For other adapaters, you can check out this package, which provides MANY adapters.

Step1: Installation of socialite package

Install socialite using the below command:

Step2: Configuration

In order for oAuth to work add the credentials of oAuth service provider in your applications config/services.php.

For example, if you want to add for Facebook and GitHub it will look like this:

Step3: Migrations

You will need a table to store the provider’s id for each user. This will help us to identify a user from their provider’s id.
Our migration Up function will look like this:

Note: Instead of defining another table you can add columns like facebook_id, github_id in users table itself. But with increasing number of providers, the columns will increase. I prefer having a separate table to keep things simple while checking during login, as we will look into it below steps.

Step4: Defining function & Adding routes.

You will need 2 functions for socialite to work:

  1. To redirect to the oAuth service provider from our application with the credentials
  2. To handle the callback from the service provider where we will retrieve users information.

In Auth\LoginController.php define a function to redirect to the provider:

And the function to handle callback from the provider. This function will do multiple things:

  1. Check if the user is already registered by checking for provider_user_id with $providerUser->getId()
  2. If the user is already registered then redirect to the intended page.
  3. If the user is not present, add the user details in users table and also add the provider’s id into social_accounts table.

Add the related routes for them:

Note: Don’t include these routes in Auth Middleware.

Step5: Adding links in Registration/Login view:

In your registration and Login view add links for the providers you want to integrate.
For example, if you want facebook and gitHub, you will have to add the like this below:

If you’re using bootstrap you can include Bootstrap Social button for nice looking buttons.

With this your Social media login and Registration is ready!!

Try register and logging using your social media, it will work. In case it won’t check if you have added the correct client_id, client_secret and also provided a correct callback URL in your oAuth service provider app.

Having some other problem? feel free to ask below.

If you have a better way of doing any of the things mentioned above, please do comment below.

 

No Comments

Leave a comment

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