Filament Socialite

repository·main·Indexed 42 days ago

https://github.com/dutchcodingcompany/filament-socialite

Adds OAuth2 social login capabilities to Filament panels using Laravel Socialite. It enables authentication via providers like GitLab and GitHub, handles user registration, and provides a configurable plugin for managing providers, domain allow-lists, and custom authentication events.

Tokens
7.6K
Snippets
28
Records
33
Agent score
53%

What's inside Filament Socialite

  1. Migrate from v0.x.x to v1.x.x (Filament v3.x)

    main

    To upgrade to v1, you must republish the configuration and register the plugin in your Panel Provider.

    sail artisan vendor:publish --provider="DutchCodingCompany\FilamentSocialite\FilamentSocialiteServiceProvider"

    In your App\Providers\Filament\YourPanelProvider:

    ->plugins([
        \DutchCodingCompany\FilamentSocialite\FilamentSocialitePlugin::make()
    ])
  2. Configure the registration flow

    main

    To support automatic account creation, ensure the password attribute on your User model is nullable in your database migration:

    $table->string('password')->nullable();

    You can also control registration via a callback in the plugin configuration. For example, to only allow registration if a user already exists:

    ->registration(fn (string $provider, SocialiteUserContract $oauthUser, ?Authenticatable $user) => (bool) $user)
  3. Configure the FilamentSocialitePlugin

    main

    Register the plugin in your Filament panel provider (e.g., app/Providers/Filament/AdminPanelProvider.php). You must provide a list of providers that correspond to your config/services.php configuration.

    use DutchCodingCompanyilament-socialite\FilamentSocialitePlugin;
    use DutchCodingCompany\filament-socialite\Provider;
    use Filament\Support\Colors;
    
    // ... inside your panel provider
    ->plugin(
        FilamentSocialitePlugin::make()
            ->providers([
                Provider::make('gitlab')
                    ->label('GitLab')
                    ->icon('fab-gitlab')
                    ->color(Color::hex('#2f2a6b'))
                    ->outlined(false)
                    ->stateless(false)
                    ->scopes(['...'])
                    ->with(['...']),
            ])
            ->slug('admin') // optional: override panel slug for oauth routes
            ->registration(true) // optional: enable user registration
            ->userModelClass(\App\�Models\\User::class) // optional: change associated model
            ->socialiteUserModelClass(\App\Models\SocialiteUser::class) // optional: change socialite model
    );
  4. Configure OAuth callback URLs

    main

    The package automatically adds two routes per panel for the OAuth flow. You must configure these in your external OAuth application settings.

    Panel-specific callback: If your panel ID is admin and the provider is github, the URL is: https://example.com/admin/oauth/callback/github

    Multi-panel callback: For stateful OAuth apps, you can use a single callback URL for multiple panels. The panel ID is determined via an encrypted state parameter: https://example.com/oauth/callback/github

  5. Migrate from v2.x.x to v3.x.x (Filament v4.x)

    main

    In version 3, the plugin uses the panel's configured path instead of its ID to generate callback URLs. If you need to revert to the previous behavior (using the panel ID), you must manually set the slug on the plugin configuration.

    FilamentSocialitePlugin::make()
        ->slug('admin') // change this to the panel's ID
        // other config for plugin
    )
  6. Migrate from v1.x.x to v2.x.x (Filament v3.x)

    main

    Version 2 refactored the plugin to align with Filament naming conventions and moved several callbacks from the AppServiceProvider directly onto the plugin instance for per-panel configuration.

    To automate the renaming of most methods, you can use the provided Rector script.

    vendor/bin/upgrade-v2
  7. Install filament-socialite

    main

    Install the package via Composer and publish the necessary migrations and configuration files.

    Note on versions:

    • Filament ^5.0.0 requires package ^3.1
    • Filament ^4.0.0 requires package 3.x.x
    • Filament ^3.2.44 (SPA mode) requires package 2.x.x or ^1.3.1
    • Filament 3.x requires package 1.x.x
    • Filament 2.x requires package 0.x.x
    composer require dutchcodingcompany/filament-socialite
    
    php artisan vendor:publish --tag="filament-socialite-migrations"
    php artisan migrate
  8. Automatic Login Button Injection via Render Hook

    main
    The package automatically registers a render hook at panels::auth.login.form.after. If the filament-socialite plugin is installed on the current Filament panel, the social login buttons will be automatically rendered below the standard login form. The visibility of the divider between the social buttons and the standard login form is controlled by the plugin's showDivider setting.
  9. Customize the registration flow with a Closure

    main

    The registration() method allows you to define custom logic for whether a user is allowed to register via a social provider. You can pass a Closure that receives the provider name, the Socialite OAuth user, and the existing application user (if any). The closure must return a bool indicating if the registration is permitted.

    Signature: Closure(string $provider, \Laravel\Socialite\Contracts\User $oauthUser, ?\Illuminate\Contracts\Auth\Authenticatable $user): bool

    ->registration(function (string $provider, $oauthUser, $user): bool {
        // Example: Only allow registration if the email domain is allowed
        return str_ends_with($oauthUser->getEmail(), '@example.com');
    })
  10. Configure Providers in v2.x.x (Filament v3.x)

    main

    In v2, providers are no longer configured via plain arrays. They must be instantiated using the Provider class. Scopes and optional parameters (previously in services.php) are now configured directly on the Provider object within the providers() method.

    use DutchCodingCompanyilament-socialite\FilamentSocialitePlugin;
    use DutchCodingCompany\filament-socialite\Provider;
    
    FilamentSocialitePlugin::make()
        ->providers([
            Provider::make('gitlab')
                ->label('GitLab')
                ->icon('fab-gitlab')
                ->color(Color::hex('#2f2a6b'))
                ->scopes([
                    'read:user',
                    'public_repo',
                ])
                ->with([
                     'hd' => 'example.com',
                ]),
        ]),