laravel/socialite
Laravel Socialite adds a clean, fluent OAuth authentication layer for Laravel. Supports Bitbucket, Facebook, GitHub, GitLab, Google, LinkedIn, Slack, Twitch, and X, handling the boilerplate for social login and user retrieval.
Installation:
composer require laravel/socialite
Publish the config file:
php artisan vendor:publish --provider="Laravel\Socialite\SocialiteServiceProvider"
Configure Providers:
Edit config/services.php with your OAuth credentials (e.g., for Google):
'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect' => env('GOOGLE_REDIRECT_URI'),
],
First Use Case: Add a route to redirect to Google OAuth:
use Laravel\Socialite\Facades\Socialite;
Route::get('/login/google', function () {
return Socialite::driver('google')->redirect();
});
Handle the callback:
Route::get('/login/google/callback', function () {
$user = Socialite::driver('google')->user();
// Use $user data (e.g., email, name, avatar)
return redirect('/dashboard');
});
config/services.php for available drivers (Google, GitHub, Facebook, etc.).Socialite::fake() for unit tests (added in v5.24.0).Redirect to Provider:
return Socialite::driver('github')->redirect();
Handle Callback:
$user = Socialite::driver('github')->user();
Laravel\Socialite\Contracts\User object with raw data from the provider.Map User Data:
$authUser = User::updateOrCreate(
['email' => $user->email],
[
'name' => $user->name,
'avatar' => $user->avatar_original,
]
);
Scopes:
Configure scopes in config/services.php or dynamically:
Socialite::driver('github')->scopes(['repo', 'user:email'])->redirect();
Custom User Fields:
Extend the User model or use a mapper:
$userData = $user->getOriginal();
// Custom logic to map fields.
State Management: Manually validate state tokens if needed:
$state = request()->query('state');
if (!hash_equals(session('oauth_state'), $state)) {
abort(403);
}
Token Refresh:
Use the refreshToken() method (v5.11.0+):
$token = Socialite::driver('google')->refreshToken($refreshToken);
auth:socialite or custom middleware.SocialiteWasCalled) for logging/auditing.Socialite::fake()->shouldReceive('user')->andReturn($fakeUser);
State Token Mismatch:
state parameter in callbacks to prevent CSRF attacks.hash_equals() for constant-time comparison (v5.26.1+).Provider-Specific Quirks:
null for email if not requested.Configuration Errors:
redirect URI in config/services.php will cause silent failures.Socialite::driver()->getConfig() to debug config issues.PKCE (OAuth 2.0):
Socialite::driver('github')->enablePKCE(false);
\Log::info('Socialite User Data:', $user->getOriginal());
Socialite::driver('slack')->withHeaders(['Authorization' => 'Bearer token']);
Custom Providers: Use Socialite Providers for unsupported platforms. Example structure:
app/Providers/
MyCustomProvider.php (extends Laravel\Socialite\Providers\OAuth2\AbstractProvider)
User Mappers: Create a mapper class to transform provider data:
class GitHubUserMapper implements UserMapperInterface {
public function map(User $user): array {
return [
'email' => $user->getEmail(),
'name' => $user->getName(),
];
}
}
Register it in AuthServiceProvider:
Socialite::extend('github', function ($app) {
return new GitHubProvider($app, new GitHubUserMapper());
});
Testing Fakes: Mock responses for unit tests:
Socialite::fake([
'provider' => 'github',
'user' => ['id' => 123, 'name' => 'Test User'],
]);
Socialite::driver('github')->scopes(['repo'])->stateless();
$user = Socialite::driver('google')->stateless()->user();
.env variables are loaded (e.g., GOOGLE_CLIENT_ID). Use:
php artisan config:clear
if changes aren’t reflected.Socialite instance in a single request to avoid re-fetching config.
How can I help you explore Laravel packages today?