Giter Club home page Giter Club logo

filament-socialite's Introduction

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:

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 {
        //
    }
}

Multi guard/model/panel implementation

Modify socialite_users table. Update user_id to user morph column

   {
        Schema::create('socialite_users', function (Blueprint $table) {
            $table->id();

            $table->morphs('user');
            $table->string('provider');
            $table->string('provider_id');

            $table->timestamps();

        });
    }

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

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

Lastly, you'll have to prepare your User model by adding the following interface and trait, which will add the necessary methods to your model:

use DutchCodingCompany\FilamentSocialite\Models\Concerns\MorphableSocialite;
use DutchCodingCompany\FilamentSocialite\Models\Contracts\MorphableSocialite as FilamentSocialiteContract;

// Add the interface:
class User extends Authenticatable implements FilamentSocialiteContract
{
    // Add the trait:
    use MorphableSocialite;
}

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.

filament-socialite's People

Contributors

andresilvagomez avatar bert-w avatar bramr94 avatar caendesilva avatar dependabot[bot] avatar dododedodonl avatar github-actions[bot] avatar iotron avatar kykurniawan avatar marcoboers avatar oyepez003 avatar phh avatar shaqaruden avatar wivaku avatar wychoong avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.