mckenziearts/laravel-oauth
Laravel package providing OAuth authentication integration, enabling your app to act as an OAuth client for third-party providers. Includes configuration helpers and middleware to streamline login, token handling, and user retrieval in Laravel projects.
composer require mckenziearts/laravel-oauth
php artisan vendor:publish --provider="LaravelOAuth\LaravelOAuthServiceProvider"
config/oauth.php with default provider keys (Facebook, Twitter, Google, etc.).config/oauth.php and add your OAuth app credentials (e.g., client_id, client_secret, redirect).'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect' => env('GOOGLE_REDIRECT_URI'),
],
.env:
GOOGLE_CLIENT_ID=your_client_id
GOOGLE_CLIENT_SECRET=your_client_secret
GOOGLE_REDIRECT_URI=http://your-app.com/auth/google/callback
LaravelOAuth\LaravelOAuthServiceProvider::class is in config/app.php under providers.routes/web.php):
Route::get('/auth/google', 'Auth\LoginController@redirectToGoogle');
Route::get('/auth/google/callback', 'Auth\LoginController@handleGoogleCallback');
Auth/LoginController.php):
use LaravelOAuth\LaravelOAuth;
public function redirectToGoogle()
{
return LaravelOAuth::driver('google')->redirect();
}
public function handleGoogleCallback()
{
try {
$user = LaravelOAuth::driver('google')->getUser();
// Handle user data (e.g., create/update user in DB)
return redirect()->route('dashboard');
} catch (\Exception $e) {
return redirect()->route('login')->with('error', 'Google auth failed: ' . $e->getMessage());
}
}
resources/views/auth/login.blade.php):
<a href="{{ route('auth.google') }}" class="btn btn-google">
Login with Google
</a>
LaravelOAuth facade to abstract provider logic.// Redirect to provider
return LaravelOAuth::driver('facebook')->redirect();
// Handle callback
$user = LaravelOAuth::driver('facebook')->getUser();
users table with a provider_id column.$userData = [
'name' => $user->name,
'email' => $user->email,
'provider_id' => $user->id,
'provider' => 'google',
];
User::updateOrCreate(
['email' => $userData['email']],
$userData
);
updateOrCreate to avoid duplicate users.auth middleware or custom logic.Route::middleware(['auth:social'])->group(function () {
Route::get('/dashboard', 'DashboardController@index');
});
app/Http/Middleware/AuthenticateSocial.php):
public function handle($request, Closure $next)
{
if (!auth()->check() || !auth()->user()->provider) {
return redirect()->route('login');
}
return $next($request);
}
LaravelOAuth::driver('google')->revoke();
// Register a custom driver in config/oauth.php
'gitlab' => [
'driver' => \LaravelOAuth\Providers\GitLab::class,
'client_id' => env('GITLAB_CLIENT_ID'),
'client_secret' => env('GITLAB_CLIENT_SECRET'),
'redirect' => env('GITLAB_REDIRECT_URI'),
],
webpack.mix.js is configured to compile assets (e.g., OAuth buttons).HttpTests to mock OAuth callbacks:
public function testGoogleAuth()
{
$response = $this->get('/auth/google');
$response->assertRedirect('https://accounts.google.com/o/oauth2/auth');
// Mock callback response
$this->actingAs(User::find(1))
->get('/auth/google/callback')
->assertRedirect('/dashboard');
}
try {
$user = LaravelOAuth::driver('twitter')->getUser();
} catch (\Exception $e) {
\Log::error('Twitter OAuth failed: ' . $e->getMessage());
throw $e;
}
Deprecated Laravel Facades:
Auth::user() instead of auth()->user()).Missing Provider Support:
State Management:
state parameter:
Route::get('/auth/google', function () {
return LaravelOAuth::driver('google')->redirect(['state' => 'secure_state']);
});
Configuration Caching:
php artisan config:clear
php artisan cache:clear
Provider-Specific Quirks:
tweet.read).r_liteprofile scope.Laravel 6+ Compatibility:
class OAuthWrapper {
public static function getUser($provider) {
return LaravelOAuth::driver($provider)->getUser();
}
}
APP_DEBUG=true in .env to see detailed errors.$response = LaravelOAuth::driver('facebook')->getUser();
\Log::debug('Facebook response:', [$response->toArray()]);
redirect URLs in config/oauth.php match your app’s domain (including http/https)..env file for OAuth credentials to avoid leaks.LaravelOAuth::extend('google', function ($app) {
return new \LaravelOAuth\Providers\Google($app, new \App\Models\User());
});
LaravelOAuth::driver('github')->scopes(['repo', 'user']);
Route::post('/github/webhook', 'WebhookController@handleGitHubWebhook');
Route::middleware(['throttle:60,1'])->group(function () {
Route::get('/auth/google', 'AuthController@redirectToGoogle
How can I help you explore Laravel packages today?