dutchcodingcompany/filament-socialite

Social login for Filament through Laravel Socialite

1.4.1 2024-03-20 12:32 UTC

README

68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f46696c616d656e74253230536f6369616c6974652e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d4475746368436f64696e67436f6d70616e7925324666696c616d656e742d736f6369616c697465267061747465726e3d617263686974656374267374796c653d7374796c655f31266465736372697074696f6e3d4164642b4f417574682b6c6f67696e2b7468726f7567682b4c61726176656c2b536f6369616c6974652b746f2b46696c616d656e742e266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313030707826696d616765733d757365722d67726f7570

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.2.44 (if using SPA mode) ^1.3.1
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:

use Laravel\Socialite\Contracts\User as SocialiteUserContract;
use Illuminate\Contracts\Auth\Authenticatable;

// ...
->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/disable registration of new (socialite-) users.
        ->setRegistrationEnabled(true)
        // (optional) Enable/disable registration of new (socialite-) users using a callback.
        // In this example, a login flow can only continue if there exists a user (Authenticatable) already.
        ->setRegistrationEnabled(fn (string $provider, SocialiteUserContract $oauthUser, ?Authenticatable $user) => (bool) $user)
        // (optional) Change the associated model class.
        ->setUserModelClass(\App\Models\User::class)
        // (optional) Change the associated socialite class (see below).
        ->setSocialiteUserModelClass(\App\Models\SocialiteUser::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 an Authenticatable 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 (string $provider, SocialiteUserContract $oauthUser, FilamentSocialite $socialite) => $socialite->getUserModelClass()::create([
    'name' => $oauthUser->getName(),
    'email' => $oauthUser->getEmail(),
]));

FilamentSocialiteFacade::setUserResolver(fn (string $provider, SocialiteUserContract $oauthUser, FilamentSocialite $socialite) => /* ... */);

Change how a Socialite user is created or retrieved

In your plugin options in your Filament panel, add the following method:

// app/Providers/Filament/AdminPanelProvider.php
->plugins([
    FilamentSocialitePlugin::make()
        // ...
        ->setSocialiteUserModelClass(\App\Models\SocialiteUser::class)

This class should at the minimum implement the FilamentSocialiteUser interface, like so:

namespace App\Models;

use DutchCodingCompany\FilamentSocialite\Models\Contracts\FilamentSocialiteUser as FilamentSocialiteUserContract;
use Illuminate\Contracts\Auth\Authenticatable;
use Laravel\Socialite\Contracts\User as SocialiteUserContract;

class SocialiteUser implements FilamentSocialiteUserContract
{
    public function getUser(): Authenticatable
    {
        //
    }

    public static function findForProvider(string $provider, SocialiteUserContract $oauthUser): ?self
    {
        //
    }
    
    public static function createForProvider(
        string $provider,
        SocialiteUserContract $oauthUser,
        Authenticatable $user
    ): self {
        //
    }
}

Change login redirect

When your panel has multi-tenancy enabled, after logging in, the user will be redirected to their default tenant. If you want to change this behavior, you can add the setLoginRedirectCallback method in the boot method of your AppServiceProvider.php:

use DutchCodingCompany\FilamentSocialite\Models\Contracts\FilamentSocialiteUser as FilamentSocialiteUserContract;
use DutchCodingCompany\FilamentSocialite\Models\SocialiteUser;

FilamentSocialite::setLoginRedirectCallback(function (string $provider, FilamentSocialiteUserContract $socialiteUser) {
    return redirect()->intended(
        route(FilamentSocialite::getPlugin()->getDashboardRouteName())
    );
});

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:

  • InvalidState(InvalidStateException $exception): When trying to retrieve the oauth (socialite) user, an invalid state was encountered
  • Login(FilamentSocialiteUserContract $socialiteUser): When a user successfully logs in
  • Registered(FilamentSocialiteUserContract $socialiteUser): When a user and socialite user is successfully registered and logged in (when enabled in config)
  • RegistrationNotEnabled(string $provider, SocialiteUserContract $oauthUser): When a user tries to login with an unknown account and registration is not enabled
  • SocialiteUserConnected(FilamentSocialiteUserContract $socialiteUser): When a socialite user is created for an existing user
  • UserNotAllowed(SocialiteUserContract $oauthUser): When a user tries to login with an email which domain is not on the allowlist

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',
    ],
]

Optional parameters

You can add optional parameters to the request by adding a with key to the provider configuration in the config/services.php config file, for example:

'github' => [
    'client_id' => '...',
    'client_secret' => '...',
    'with' => [
        // Add optional parameters here
        'hd' => 'example.com',
    ],
]

Note: you cannot use the state parameter, as it is used to determine from which Filament panel the user came from.

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.