Installation:
composer require chrisreedio/socialment
Publish the config and migrations:
php artisan vendor:publish --provider="ChrisReedio\Socialment\SocialmentServiceProvider"
php artisan migrate
Configure Providers:
Edit config/socialment.php and add your OAuth credentials (e.g., GitHub, Google, etc.) under the providers key:
'providers' => [
'github' => [
'client_id' => env('GITHUB_CLIENT_ID'),
'client_secret' => env('GITHUB_CLIENT_SECRET'),
'redirect' => env('GITHUB_REDIRECT_URI'),
],
],
Enable in Filament Panel:
Add the Socialment widget to your Filament panel’s login page in app/Providers/Filament/AdminPanelProvider.php:
public function panel(Panel $panel): Panel
{
return $panel
->widgets([
\ChrisReedio\Socialment\Widgets\SocialmentWidget::class,
]);
}
First Use Case:
Provider Integration:
Socialment::providers() helper to fetch configured providers dynamically in Blade or PHP:
$providers = Socialment::providers();
foreach ($providers as $provider) {
echo $provider->getButton();
}
configure() method:
public function configure(): array
{
return [
'providers' => fn () => Socialment::providers()->where('enabled', true),
];
}
User Mapping:
Socialment\Events\UserCreated:
Socialment::userCreated(function (User $user, array $attributes) {
$user->update(['email_verified_at' => now()]);
});
config/socialment.php:
'fields' => [
'github' => [
'name' => 'name',
'email' => 'email',
],
],
Panel-Specific Config:
SocialmentWidget:
class CustomSocialmentWidget extends SocialmentWidget
{
protected static ?string $panel = 'admin';
protected static ?string $navigationIcon = 'heroicon-o-share-social';
}
SPA Authentication:
Socialment::redirectToProvider() method to generate signed URLs:
// Frontend (Vue/React)
const url = `/socialment/github?callback=${encodeURIComponent(window.location.href)}`;
window.location.href = url;
routes/web.php to handle the OAuth callback:
Route::get('/socialment/{provider}/callback', [SocialmentController::class, 'handleProviderCallback']);
Testing:
Socialment::fake() in tests to simulate OAuth responses:
Socialment::fake([
'github' => ['id' => 123, 'name' => 'Test User', 'email' => 'test@example.com'],
]);
$response = $this->get('/admin/login')->assertSee('Test User');
Environment Variables:
GITHUB_REDIRECT_URI (or equivalent) matches the callback URL in your provider’s dashboard. Use env('APP_URL') to dynamically generate it:
'redirect' => env('APP_URL') . '/admin/socialment/github/callback',
http://localhost to the "Authorized Callback URLs" in your OAuth provider settings.User Conflicts:
provider_id field to your users table and updating the config/socialment.php fields mapping:
'fields' => [
'github' => [
'provider_id' => 'id',
'provider_name' => 'github',
],
],
laravel/socialite to merge accounts manually.CSRF Issues:
APP_URL is consistent across environments and the VerifyCsrfToken middleware is excluded for the callback route:
Route::middleware(['web', 'throttle:60'])->group(function () {
Route::get('/socialment/{provider}/callback', [SocialmentController::class, 'handleProviderCallback']);
});
Provider-Specific Quirks:
'google' => [
'scopes' => ['profile', 'email'],
],
Log OAuth Errors:
config/socialment.php:
'debug' => env('APP_DEBUG', false),
storage/logs/laravel.log for Socialite errors (e.g., invalid credentials).Token Validation:
Socialite::with('github')->useDebugMode();
Widget Not Showing:
panel()->widgets() and that no Filament middleware (e.g., auth) is blocking the login page.Custom Providers:
ChrisReedio\Socialment\Providers\Provider to support custom OAuth services:
class CustomProvider extends Provider
{
protected string $name = 'custom';
protected string $clientIdKey = 'CUSTOM_CLIENT_ID';
public function getAuthUrl(): string
{
return 'https://custom-provider.com/oauth/authorize?client_id=' . $this->clientId;
}
}
config/socialment.php:
'providers' => [
'custom' => [
'class' => \App\Providers\CustomProvider::class,
'client_id' => env('CUSTOM_CLIENT_ID'),
],
],
Post-Auth Logic:
Socialment\Events\Login to trigger actions after successful login:
Socialment::login(function (User $user) {
event(new UserLoggedIn($user));
});
UI Customization:
resources/views/vendor/socialment/widget.blade.php) to modify button styles or add labels.button_class config option to apply Tailwind classes:
'providers' => [
'github' => [
'button_class' => 'bg-gray-800 hover:bg-gray-700',
],
],
Rate Limiting:
throttle middleware to prevent abuse:
Route::middleware(['throttle:10,1'])->group(function () {
Route::get('/socialment/{provider}/callback', [SocialmentController::class, 'handleProviderCallback']);
});
Cache Provider Config:
Socialment::providers() result if called frequently (e.g., in a widget):
$providers = Cache::remember('socialment.providers', now()->addHours(1), function () {
return Socialment::providers();
});
Lazy-Load Icons:
Icon component with lazy-loaded SVGs to reduce initial load time:
$provider->getButton()->icon('heroicon-o-globe-alt')->size('lg'),
How can I help you explore Laravel packages today?