• Stars
    star
    149
  • Rank 247,747 (Top 5 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 2 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Add OAuth login through Laravel Socialite to Filament.

Social login for Filament through Laravel Socialite

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Add OAuth2 login through Laravel Socialite to Filament. OAuth1 (eg. Twitter) is not supported at this time.

Installation

Filament version Package version
3.x 1.x.x
2.x 0.x.x

Install the package via composer:

composer require dutchcodingcompany/filament-socialite

Publish and migrate the migration file:

php artisan vendor:publish --tag="filament-socialite-migrations"
php artisan migrate

Other configuration files include:

php artisan vendor:publish --tag="filament-socialite-config"
php artisan vendor:publish --tag="filament-socialite-views"
php artisan vendor:publish --tag="filament-socialite-translations"

You need to register the plugin in the Filament panel provider (the default filename is app/Providers/Filament/AdminPanelProvider.php). The following options are available:

->plugin(
    FilamentSocialitePlugin::make()
        // (required) Add providers corresponding with providers in `config/services.php`. 
        ->setProviders([
            'github' => [
                'label' => 'GitHub',
                // Custom icon requires an additional package, see below.
                'icon' => 'fab-github',
                // (optional) Button color override, default: 'gray'.
                'color' => 'primary',
                // (optional) Button style override, default: true (outlined).
                'outlined' => false,
            ],
        ])
        // (optional) Enable or disable registration from OAuth.
        ->setRegistrationEnabled(true)
        // (optional) Change the associated model class.
        ->setUserModelClass(\App\Models\User::class)
);

See Socialite Providers for additional Socialite providers.

Icons

You can specify a Blade Icon. You can add Font Awesome brand icons made available through Blade Font Awesome by running:

composer require owenvoke/blade-fontawesome

Registration flow

This package supports account creation for users. However, to support this flow it is important that the password attribute on your User model is nullable. For example, by adding the following to your users table migration. Or you could opt for customizing the user creation, see below.

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

Domain Allowlist

This package supports the option to limit the users that can login with the OAuth login to users of a certain domain. This can be used to setup SSO for internal use.

->plugin(
    FilamentSocialitePlugin::make()
        ->setRegistrationEnabled(true)
        ->setDomainAllowList(['localhost'])
);

Changing how a (socialite) user is created or retrieved

In your AppServiceProvider.php, add in the boot method

use DutchCodingCompany\FilamentSocialite\Facades\FilamentSocialite as FilamentSocialiteFacade;
use DutchCodingCompany\FilamentSocialite\FilamentSocialite;
use Laravel\Socialite\Contracts\User as SocialiteUserContract;

// Default
FilamentSocialiteFacade::setCreateUserCallback(fn (SocialiteUserContract $oauthUser, FilamentSocialite $socialite) => $socialite->getUserModelClass()::create([
    'name' => $oauthUser->getName(),
    'email' => $oauthUser->getEmail(),
]));

One can set a callback to customize the following actions:

  • Create the filament user: FilamentSocialite::setCreateUserCallback()
  • Create the socialite user: FilamentSocialite::setCreateSocialiteUserCallback()
  • Resolve the regular user: FilamentSocialite::setUserResolver()

See FilamentSocialite.php.

Filament Fortify

This component can also be added while using the Fortify plugin plugin.

## in Service Provider file
public function boot()
{
    //...
    
    Filament::registerRenderHook(
        'filament-fortify.login.end',
        fn (): string => Blade::render('<x-filament-socialite::buttons />'),
    );
}

Filament Breezy

This component can also be added while using the Breezy plugin plugin.

You can publish the login page for Filament Breezy by running:

php artisan vendor:publish --tag="filament-breezy-views"

Which produces a login page at resources/views/vendor/filament-breezy/login.blade.php.

You can then add the following snippet in your form:

<x-filament-socialite::buttons />

Events

There are a few events dispatched during the authentication process:

  • Login: When a user successfully logs in
  • Registered: When a user is successfully registered and logged in (when enabled in config)
  • UserNotAllowed: When a user tries to login with an email which domain is not on the allowlist
  • RegistrationNotEnabled: When a user tries to login with an unknown account and registration is not enabled
  • InvalidState: When trying to retrieve the oauth (socialite) user, an invalid state was encountered

Scopes

Scopes should be added in your config/services.php config file, for example:

'github' => [
    'client_id' => '...',
    'client_secret' => '...',
    'scopes' => [
        // Add scopes here.
        'read:user',
        'public_repo',
    ],
]

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.