directorytree/bartender
Opinionated Laravel Socialite auth starter. Ships ready-made routes (/auth/{driver}/redirect, /callback), controller, and user columns for provider ID/name plus optional access/refresh tokens. Highly customizable; supports soft deletes and email verification.
Installation:
composer require directorytree/bartender
php artisan vendor:publish --provider="DirectoryTree\Bartender\BartenderServiceProvider"
php artisan migrate
Configure Routes:
In routes/web.php:
use DirectoryTree\Bartender\Facades\Bartender;
Bartender::routes();
Register Providers:
In AppServiceProvider:
Bartender::serve('google'); // Replace 'google' with your provider
Add Login Links (Blade):
<a href="{{ route('auth.driver.redirect', 'google') }}">Login with Google</a>
dashboard).Provider Registration:
Bartender::serve('provider_name') in AppServiceProvider to enable a provider (e.g., Google, Microsoft).config/services.php with the correct redirect URL (e.g., /auth/google/callback).Customizing User Creation:
ProviderRepository to customize user creation logic (e.g., linking existing users, setting attributes).AppServiceProvider:
$this->app->bind(ProviderRepository::class, CustomUserProviderRepository::class);
Handling Tokens:
StoresProviderTokens in your User model to store access/refresh tokens.$hidden and $casts:
protected $hidden = ['provider_access_token', 'provider_refresh_token'];
protected function casts(): array { return ['provider_access_token' => 'encrypted']; }
Redirect Logic:
ProviderRedirector.AppServiceProvider:
$this->app->bind(ProviderRedirector::class, CustomProviderRedirector::class);
Scopes and Provider-Specific Logic:
UserProviderHandler for provider-specific behavior (e.g., scopes for Microsoft):
class MicrosoftUserHandler extends UserProviderHandler {
public function redirect(Provider $provider, string $driver): RedirectResponse {
$provider->scopes(['Mail.ReadWrite']);
return parent::redirect($provider, $driver);
}
}
Bartender::serve('microsoft', MicrosoftUserHandler::class);
socialiteproviders/google).Session::regenerate();
Bartender::fake() in tests to mock provider responses:
Bartender::fake('google')->shouldReturnUser([
'id' => 123,
'name' => 'Test User',
'email' => '[email protected]',
]);
Missing Provider Setup:
Driver [X] not supported.socialiteproviders/google) and registered it with Bartender::serve().Route Requirements:
Routing requirement for "driver" cannot be empty.Bartender::serve() in AppServiceProvider.Token Storage:
2024_10_27_131354_add_provider_token_columns_to_users_table.php and omit StoresProviderTokens.Guarded Attributes:
$guarded in the model.forceFill() in your custom ProviderRepository:
$user->forceFill(['email' => $user->email])->save();
Email Verification:
updateOrCreate() in ProviderRepository.dd($socialiteUser) in a custom ProviderHandler to inspect the raw provider data.dd($driver) in ProviderRedirector methods to debug redirect paths.Bartender::fake() to simulate provider responses without hitting external APIs.Custom User Model:
Bartender::setUserModel(User::class) if it’s not in App\Models\User.Password Handling:
hashPassword() in ProviderRepository to use a custom hashing mechanism:
public function hashPassword(string $password): string {
return Hash::make($password . '_custom_salt');
}
Soft Deletes:
exists() in ProviderRepository:
public function exists(string $driver, SocialiteUser $user): bool {
return User::where('email', $user->email)->doesntExist(); // Custom logic
}
Multi-Provider Merging:
ProviderRepository to merge users from multiple providers (e.g., Google + Microsoft):
public function updateOrCreate(string $driver, SocialiteUser $user): Authenticatable {
return User::firstOrCreate(
['email' => $user->email],
['provider_id' => $user->id, 'provider_name' => $driver]
);
}
withTrashed() sparingly in ProviderRepository to minimize queries.provider_access_token and provider_refresh_token if storing them.openid profile email if you only need email).
How can I help you explore Laravel packages today?