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.



  • Maximiliano Reply

    Where is the definition of SocialAccount in createOrGetUser() ?, or it has something to do with socialite, or maybe I'm not understanding well how it works the database-migration-model .

    2018-06-22 08:26:36
    • Nikhil Agrawal

      SocialAccount is the name of model for social_accounts table as mentioned in step2.

      2018-06-22 10:16:29
  • asdadasd Reply

    you forget to show the SocialAccount method man what the heck

    2017-12-02 22:25:28
    • Nikhil Agrawal

      SocialAccount is the name of Model. Check the migration for the table in Step2.

      2018-06-22 10:20:01

Leave a comment

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