cresjie/social-login
Laravel 4 package for OAuth2 social authentication with Google, Facebook, Yahoo, and GitHub. Provides routes for redirect/login and callback authentication, returns user data, and supports config-based result filtering to normalize fields across providers.
Installation:
composer require cresjie/social-login
Publish the config file:
php artisan vendor:publish --provider="Cresjie\SocialLogin\SocialLoginServiceProvider"
Configuration:
Edit config/social-login.php with your OAuth credentials (e.g., Google/Facebook API keys, secrets, and redirect URIs).
First Use Case: Add a route for social login (e.g., Google):
Route::get('/auth/google', 'Auth\SocialLoginController@redirectToProvider');
Route::get('/auth/google/callback', 'Auth\SocialLoginController@handleProviderCallback');
Controller Setup: Use the provided controller or extend it:
use Cresjie\SocialLogin\Facades\SocialLogin;
public function redirectToProvider($provider)
{
return SocialLogin::driver($provider)->redirect();
}
public function handleProviderCallback($provider)
{
$user = SocialLogin::driver($provider)->getUser();
// Handle user creation/login logic here
}
User Authentication Flow:
return SocialLogin::driver('google')->redirect();
$user = SocialLogin::driver('google')->getUser();
$existingUser = User::where('email', $user->email)->first();
if (!$existingUser) {
$existingUser = User::create([
'name' => $user->name,
'email' => $user->email,
'provider_id' => $user->id,
'provider' => 'google',
]);
}
Auth::login($existingUser);
Custom User Mapping:
Override the default user mapping by extending the SocialLogin facade or using a custom callback:
SocialLogin::extend('github', function($app) {
$app->useCustomUserMapping(function ($providerUser) {
return [
'name' => $providerUser->name,
'email' => $providerUser->email,
'avatar' => $providerUser->avatar_url,
];
});
});
Session Handling: Use Laravel's built-in session to persist user data after login:
session(['social_login_data' => $user->toArray()]);
Multi-Provider Support: Dynamically handle multiple providers in a single route:
public function handleSocialLogin($provider)
{
try {
$user = SocialLogin::driver($provider)->getUser();
// Logic for user creation/login
} catch (\Exception $e) {
return redirect()->back()->with('error', $e->getMessage());
}
}
Deprecated Package:
use Illuminate\Auth\AuthManager (deprecated in Laravel 8+).Session or Redirect facade usage (updated in newer Laravel versions).OAuth 2.0 Changes:
config/social-login.php endpoints against the latest provider documentation. Example for Google:
'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect' => env('GOOGLE_REDIRECT_URI'),
'scope' => ['email', 'profile'], // Update scopes as needed
],
State Parameter:
$state = Str::random(40);
session(['oauth_state' => $state]);
return SocialLogin::driver('google')->setState($state)->redirect();
In the callback:
if (session('oauth_state') !== $providerUser->getState()) {
throw new \Exception('State mismatch');
}
User Data Format:
getUser() method may return raw provider data in an unexpected format.$user->toArray() or dump the object to understand the structure:
dd(SocialLogin::driver('github')->getUser());
Enable Debugging:
Add this to config/social-login.php to log provider responses:
'debug' => env('APP_DEBUG', false),
Provider-Specific Errors:
redirect in config matches your app's URL.Testing Locally: Use tools like ngrok to expose your local dev server for OAuth callbacks:
ngrok http 8000
Update the redirect URI in config/social-login.php to include your ngrok URL (e.g., https://abc123.ngrok.io/auth/google/callback).
Custom Providers:
Add support for unsupported providers (e.g., Twitter, LinkedIn) by extending the SocialLogin facade:
SocialLogin::extend('twitter', function($app) {
$app->useProvider(\Cresjie\SocialLogin\Providers\TwitterProvider::class);
});
Post-Login Redirects: Override the default redirect logic in your callback:
return redirect()->intended('dashboard'); // Use Laravel's intended redirect
User Sync Logic: Create a service to handle user creation/login consistently:
class SocialAuthService {
public function handleLogin($providerUser, $providerName) {
$user = User::firstOrCreate([
'provider_id' => $providerUser->id,
'provider' => $providerName,
], [
'name' => $providerUser->name,
'email' => $providerUser->email,
]);
Auth::login($user);
return $user;
}
}
Rate Limiting: Add rate limiting to prevent abuse of social login endpoints:
Route::middleware(['throttle:10,1'])->group(function () {
Route::get('/auth/google', 'Auth\SocialLoginController@redirectToProvider');
});
How can I help you explore Laravel packages today?