Easily Add Social Logins to Your App with Socialite

Laravel Socialite is a package developed to abstract away any social authentication complexities and boilerplate code into a fluent and expressive interface.

Socialite merging social networks

Socialite only supports Google, Facebook, Twitter, LinkedIn, Github, and Bitbucket as OAuth providers. They won’t be adding any others to the list, however, there’s a community-driven collection called Socialite Providers, which contains plenty of unofficial providers for Socialite. More on this in the next section.

I’ll assume you already have a fresh Laravel application instance up and running on your machine, so you can see the code in action along the way. If you need a good development environment, you’re free to use Homestead Improved.

Form Based Authentication

Before getting into OAuth authentication, let’s set up Laravel’s standard form based authentication. To do this, we run the make:auth artisan command, which installs all the necessary views as well as the required authentication endpoints.

php artisan make:auth

Note We also need to run php artisan migrate to make sure the users table is created.

Now, if we head over to /login, we should see a nice Bootstrap-styled login page that works.

Laravel form based authentication

Adding Social Authentication

To get started with Socialite, we install it with Composer:

composer require laravel/socialite

Once installed, Socialite’s service provider and facade should be registered in config/app.php – just like with any other Laravel package.

config/app.php

<?php

// ...

'providers' => [

        // ...

        /*
         * Package Service Providers...
         */      
        LaravelSocialiteSocialiteServiceProvider::class,

    ],

// ...

And here’s the facade alias:

<?php

// ...
'aliases' => [

        // ...

        'Socialite' => LaravelSocialiteFacadesSocialite::class,

    ],
// ...

Socialite is registered as a lazy-loaded singleton service inside the service container.

Configuration

To use any provider, we need to register an OAuth application on that provider platform. In return, we’ll be given a pair of client ID and client secret keys as our credentials for interacting with the provider’s API.

We need to add the credentials in config/services.php for each provider:

// ...

'facebook' => [
        'client_id'     => env('FB_CLIENT_ID'),
        'client_secret' => env('FB_CLIENT_SECRET'),
        'redirect'      => env('FB_URL'),
],

'twitter' => [
        'client_id'     => env('TWITTER_CLIENT_ID'),
        'client_secret' => env('TWITTER_CLIENT_SECRET'),
        'redirect'      => env('TWITTER_URL'),
],

'github' => [
        'client_id'     => env('GITHUB_CLIENT_ID'),
        'client_secret' => env('GITHUB_CLIENT_SECRET'),
        'redirect'      => env('GITHUB_URL'),
],

// ...

The actual key values are put into the .env file in the project’s root directory.

Database Considerations

Since the users table structure hasn’t been designed to integrate social authentications, we first need to do a few tweaks there.

Continue reading %Easily Add Social Logins to Your App with Socialite%


Source: Sitepoint