dutchcodingcompany / filament-socialite
Social login for Filament through Laravel Socialite
Installs: 43 585
Dependents: 2
Suggesters: 0
Security: 0
Stars: 120
Watchers: 9
Forks: 34
Open Issues: 0
Requires
- php: ^8.1
- filament/filament: ^3.0.9
- illuminate/contracts: ^10.0|^11.0
- laravel/socialite: ^5.5
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- larastan/larastan: ^2.9
- nunomaduro/collision: ^7.0|^8.1
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^10.0
Suggests
- dev-main
- 1.4.1
- 1.4.0
- 1.3.1
- 1.3.0
- 1.2.0
- v1.1.1
- 1.1.0
- 1.0.1
- 1.0.0
- 0.2.6
- 0.2.5
- 0.2.4
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- dev-dependabot/github_actions/dependabot/fetch-metadata-2.0.0
- dev-laravel-11-support
- dev-bert-w-patch-2
- dev-bert-w-patch-1
- dev-bramr94-patch-3
- dev-bramr94-patch-1
- dev-bramr94-patch-2
- dev-54-change-registration
- dev-support-filament-v3
- dev-fix/user-model-resolving
- dev-fix/missing-variable-for-registered-event
- dev-feature/add-login-events-dispatching
This package is auto-updated.
Last update: 2024-03-26 11:22:19 UTC
README
Social login for Filament through Laravel Socialite
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 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
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.