/Manager

Easily add new or override built-in providers in Laravel Socialite

Primary LanguagePHPMIT LicenseMIT

Socialite Providers Manager

Build Status Code Coverage Scrutinizer Code Quality Latest Stable Version Total Downloads Latest Unstable Version License

Contents

About

A package for Laravel Socialite that allows you to easily add new providers or override current providers.

Benefits

  • You will have access to all of the providers that you load in using the manager.
  • Instantiation is deferred until Socialite is called
  • You can override current providers
  • You can create new providers

Available Providers

Installation

1. Composer

Note: You will not need to do this if you require one of the available providers.

// This assumes that you have composer installed globally
composer require socialiteproviders/manager

2. Service Provider

  • Remove Laravel\Socialite\SocialiteServiceProvider from your providers[] array in config\app.php if you have added it already.

  • Add SocialiteProviders\Manager\ServiceProvider to your providers[] array in config\app.php.

For example:

'providers' => [
    // a whole bunch of providers
    // remove 'Laravel\Socialite\SocialiteServiceProvider',
    'SocialiteProviders\Manager\ServiceProvider', // add
];

3. Add the Event and Listeners

  • Add SocialiteProviders\Manager\SocialiteWasCalled to your listen[] array in <app_name>/Providers/EventServiceProvider.

  • Add your listeners (i.e. the ones from the providers) to the SocialiteProviders\Manager\SocialiteWasCalled[] that you just created.

  • Note: You do not need to add anything for the built-in socialite providers unless you override them with your own providers.

For example:

/**
 * The event handler mappings for the application.
 *
 * @var array
 */
protected $listen = [
    `SocialiteProviders\Manager\SocialiteWasCalled` => [
        'Your\Name\Space\ProviderNameExtendSocialite@handle', // the listener for the provider that you made
        'SocialiteProviders\Meetup\MeetupExtendSocialite@handle', // the listener for an actual provider
        // This is where we define all of our ExtendSocialite listeners (i.e. new providers)
    ],
];

Reference

4. Services Array and .env

Note: In these examples, you need to replace the providername with the actual name of the provider.

  • Add your provider to config/services.php.
'providername' => [
    'client_id' => env('PROVIDERNAME_KEY'),
    'client_secret' => env('PROVIDERNAME_SECRET'),
    'redirect' => env('PROVIDERNAME_REDIRECT_URI'),
]
  • Append provider values to your .env file
// other values above
PROVIDERNAME_KEY=yourkeyfortheservice
PROVIDERNAME_SECRET=yoursecretfortheservice
PROVIDERNAME_REDIRECT_URI=https://example.com/login

Reference

Usage

You should now be able to use it like you would regularly use socialite:

return Socialite::with('providername')->redirect(); 
// replace providername with your provider name!

Reference

Creating a Handler

Below is an example handler. You need to add this full class name to the listen[] in the EventServiceProvider.

namespace Your\Name\Space;

use SocialiteProviders\Manager\SocialiteWasCalled;

class ProviderNameExtendSocialite
{
    public function handle(SocialiteWasCalled $socialiteWasCalled)
    {
        $socialiteWasCalled->extendSocialite('providername', 'Your\Name\Space\ProviderName');
    }
}

Reference

Creating a Provider

Overriding a Built-in Provider

You can easily override a built-in laravel/socialite provider by creating a new one with exactly the same name.