socialiteproviders/manager
Laravel SocialiteProviders Manager lets you add or override Socialite OAuth providers with deferred loading, easy Lumen support, configurable stateless mode, dynamic config overrides, and direct .env variable retrieval for simpler setup.
composer require socialiteproviders/manager
php artisan vendor:publish --provider="SocialiteProviders\Manager\ManagerServiceProvider"
config/app.php (automatically added in Laravel 5.5+):
'providers' => [
// ...
SocialiteProviders\Manager\ManagerServiceProvider::class,
],
composer require socialiteproviders/github
.env:
GITHUB_CLIENT_ID=your_client_id
GITHUB_CLIENT_SECRET=your_client_secret
GITHUB_REDIRECT_URI=http://your-app.com/auth/github/callback
use Laravel\Socialite\Facades\Socialite;
return Socialite::driver('github')->redirect();
Socialite::driver('github') is called.composer require socialiteproviders/{provider-name}
Example: socialiteproviders/discord.
.envDISCORD_CLIENT_ID=your_id
DISCORD_CLIENT_SECRET=your_secret
DISCORD_REDIRECT_URI=http://your-app.com/auth/discord/callback
// Redirect to Discord OAuth
return Socialite::driver('discord')->redirect();
// Handle callback
$user = Socialite::driver('discord')->user();
To override a default provider (e.g., Facebook), create a custom provider with the same name (e.g., facebook).
Example: Custom Facebook provider with extended scopes.
// app/Providers/SocialiteServiceProvider.php
use SocialiteProviders\Manager\SocialiteWasCalled;
public function boot()
{
Socialite::extend('facebook', function ($app) {
$config = $app->make('config')->get('services.facebook');
$config['scopes'] = ['email', 'public_profile', 'user_friends']; // Extended scopes
return Socialite::buildProvider(\SocialiteProviders\Facebook\FacebookExtender::class, $config);
});
}
Pass runtime configurations (e.g., per-tenant credentials or conditional stateless mode):
$clientId = 'tenant_specific_id';
$clientSecret = 'tenant_specific_secret';
$redirectUri = 'https://tenant.example.com/auth/callback';
$config = new \SocialiteProviders\Manager\Config($clientId, $clientSecret, $redirectUri);
return Socialite::driver('github')
->setConfig($config)
->stateless() // Optional: Disable session storage
->redirect();
Extend the provider to include the full response body (e.g., refresh_token):
// In your custom provider class (extends \SocialiteProviders\Manager\OAuth2\AbstractProvider)
protected function getAccessTokenResponse($code)
{
$response = parent::getAccessTokenResponse($code);
$this->user['accessTokenResponseBody'] = json_decode($response->getBody(), true);
return $response;
}
// Usage:
$user = Socialite::driver('github')->user();
$refreshToken = $user->accessTokenResponseBody['refresh_token'] ?? null;
The package supports stateless mode by default in Lumen:
// config/services.php
'socialite' => [
'stateless' => true, // Disable session storage
],
Extend Socialite dynamically via events (e.g., add a provider only for specific routes):
// app/Providers/EventServiceProvider.php
protected $listen = [
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
\App\Listeners\ExtendSocialiteForAdmin::class,
],
];
// Listener: Add a provider only for admin routes
public function handle(SocialiteWasCalled $event)
{
if (request()->is('admin/*')) {
$event->extendSocialite('custom-admin-provider', \App\Providers\CustomAdminProvider::class);
}
}
Route providers based on tenant context (e.g., tenant1 uses GitHub, tenant2 uses GitLab):
// Middleware or service
public function getProvider()
{
$tenant = auth()->user()->tenant;
return $tenant->provider === 'github' ? 'github' : 'gitlab';
}
// Usage
return Socialite::driver($this->getProvider())->redirect();
Provider Not Found Errors
socialiteproviders/github).composer require socialiteproviders/{provider} and ensure .env is configured.config/socialite.php for loaded providers.Stateless Mode Conflicts
stateless() after calling redirect() or user().Socialite::driver('github')->stateless()->redirect();
Event Listener Not Triggering
EventServiceProvider.$listen array and ensure the event class (SocialiteWasCalled) is imported.Dynamic Config Overrides
setConfig() must be called before redirect() or user().Socialite::driver('github')
->setConfig($config)
->redirect(); // Works
// vs.
Socialite::driver('github')->redirect()->setConfig($config); // Fails
Laravel 10+ Caching Issues
php artisan config:clear
Log Provider Instantiation
Override SocialiteWasCalled to log when providers are loaded:
public function handle(SocialiteWasCalled $event)
{
\Log::info('Provider loaded:', ['provider' => $event->providerName]);
}
Inspect Raw Responses Dump the full OAuth response for debugging:
$user = Socialite::driver('github')->user();
\Log::debug('Raw user data:', $user->toArray());
Validate .env Config
Use Laravel’s config helper to verify loaded providers:
$providers = config('services.socialite.providers', []);
\Log::debug('Loaded providers:', $providers);
Custom Provider Abstraction
Extend \SocialiteProviders\Manager\OAuth2\AbstractProvider to add:
extra['custom_field']).Provider Factory Create a dynamic provider factory for multi-tenant apps:
public function getProviderClass(string $name): string
{
return match ($name) {
'github' => \SocialiteProviders\GitHub\GitHubExtender::class,
'gitlab' => \SocialiteProviders\GitLab\GitLabExtender::class,
default => parent::getProviderClass($name),
};
}
Stateless Mode Optimization For Lumen APIs, disable session storage entirely:
// config/socialite.php
'stateless' => env('APP_ENV') !== 'local',
Provider Whitelisting Restrict available providers for security:
// app/Providers/SocialiteServiceProvider.php
public function boot()
{
Socialite::extend('github', function ($app) {
if (!in_array('github', config('socialite.allowed_providers'))) {
abort(403);
}
// ...
});
}
How can I help you explore Laravel packages today?