Add OAuth2 login through Laravel Socialite to Filament. OAuth1 (eg. Twitter) is not supported at this time.
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.
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
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();
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'])
);
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) => /* ... */);
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 {
//
}
}
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())
);
});
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 />'),
);
}
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 />
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 encounteredLogin(FilamentSocialiteUserContract $socialiteUser)
: When a user successfully logs inRegistered(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 enabledSocialiteUserConnected(FilamentSocialiteUserContract $socialiteUser)
: When a socialite user is created for an existing userUserNotAllowed(SocialiteUserContract $oauthUser)
: When a user tries to login with an email which domain is not on the allowlist
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',
],
]
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',
],
]
You can add stateless
parameters to the provider configuration in the config/services.php config file, for example:
'apple' => [
'client_id' => '...',
'client_secret' => '...',
'stateless'=>true,
]
Note: you cannot use the state
parameter, as it is used to determine from which Filament panel the user came from.
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.