dutchcodingcompany/filament-socialite 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dutchcodingcompany/filament-socialite

Composer 安装命令:

composer require dutchcodingcompany/filament-socialite

包简介

Social login for Filament through Laravel Socialite

README 文档

README

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 Readme
^5.0.0 ^3.1 Link
^4.0.0 3.x.x Link
^3.2.44 (if using SPA mode) 2.x.x Link
^3.2.44 (if using SPA mode) ^1.3.1
3.x 1.x.x Link
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 DutchCodingCompany\FilamentSocialite\FilamentSocialitePlugin;
use DutchCodingCompany\FilamentSocialite\Provider;
use Filament\Support\Colors;
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`. 
        ->providers([
            // Create a provider 'gitlab' corresponding to the Socialite driver with the same name.
            Provider::make('gitlab')
                ->label('GitLab')
                ->icon('fab-gitlab')
                ->color(Color::hex('#2f2a6b'))
                ->outlined(false)
                ->stateless(false)
                ->scopes(['...'])
                ->with(['...']),
        ])
        // (optional) Override the panel slug to be used in the oauth routes. Defaults to the panel's configured path.
        ->slug('admin')
        // (optional) Enable/disable registration of new (socialite-) users.
        ->registration(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.
        ->registration(fn (string $provider, SocialiteUserContract $oauthUser, ?Authenticatable $user) => (bool) $user)
        // (optional) Change the associated model class.
        ->userModelClass(\App\Models\User::class)
        // (optional) Change the associated socialite class (see below).
        ->socialiteUserModelClass(\App\Models\SocialiteUser::class)
);

This package automatically adds 2 routes per panel to make the OAuth flow possible: a redirector and a callback. When setting up your external OAuth app configuration, enter the following callback URL (in this case for the Filament panel with ID admin and the github provider):

https://example.com/admin/oauth/callback/github

A multi-panel callback route is available as well that does not contain the panel ID in the url. Instead, it determines the panel ID from an encrypted state input (...?state=abcd1234). This allows you to create a single OAuth application for multiple Filament panels that use the same callback URL. Note that this only works for stateful OAuth apps:

https://example.com/oauth/callback/github

If in doubt, run php artisan route:list to see which routes are available to you.

Icons

You can specify a custom icon for each of your login providers. 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 Allow list

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()
        // ...
        ->registration(true)
        ->domainAllowList(['localhost'])
);

Changing how an Authenticatable user is created or retrieved

You can use the createUserUsing and resolveUserUsing methods to change how a user is created or retrieved.

use DutchCodingCompany\FilamentSocialite\FilamentSocialitePlugin;
use Laravel\Socialite\Contracts\User as SocialiteUserContract;

->plugin(
    FilamentSocialitePlugin::make()
        // ...
        ->createUserUsing(function (string $provider, SocialiteUserContract $oauthUser, FilamentSocialitePlugin $plugin) {
            // Logic to create a new user.
        })
        ->resolveUserUsing(function (string $provider, SocialiteUserContract $oauthUser, FilamentSocialitePlugin $plugin) {
            // Logic to retrieve an existing user.
        })
        ...
);

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()
        // ...
        ->socialiteUserModelClass(\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 {
        //
    }
}

Check if the user is authorized to use the application

You can use the authorizeUserUsing method to check if the user is authorized to use the application. Note: by default this method check if the user's email domain is in the domain allow list.

use DutchCodingCompany\FilamentSocialite\FilamentSocialitePlugin;
use Laravel\Socialite\Contracts\User as SocialiteUserContract;

->plugin(
    FilamentSocialitePlugin::make()
        // ...
        ->authorizeUserUsing(function (FilamentSocialitePlugin $plugin, SocialiteUserContract $oauthUser) {
            // Logic to authorize the user.
            return FilamentSocialitePlugin::checkDomainAllowList($plugin, $oauthUser);
        })
        // ...
);

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 call the 'redirectAfterLoginUsing' method on the FilamentSocialitePlugin.

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

FilamentSocialitePlugin::make()
    ->redirectAfterLoginUsing(function (string $provider, FilamentSocialiteUserContract $socialiteUser, FilamentSocialitePlugin $plugin) {
        // Change the redirect behaviour here.
    });

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(string $provider, SocialiteUserContract $oauthUser, FilamentSocialiteUserContract $socialiteUser): When a user and socialite user is successfully registered and logged in (when enabled in config)
  • RegistrationNotEnabled(string $provider, SocialiteUserContract $oauthUser, ?Auhthenticatable $user): When a user tries to login with an unknown account and registration is not enabled
  • SocialiteUserConnected(string $provider, SocialiteUserContract $oauthUser, 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 can be added to the provider on the panel, for example:

use DutchCodingCompany\FilamentSocialite\FilamentSocialitePlugin;
use DutchCodingCompany\FilamentSocialite\Provider;

FilamentSocialitePlugin::make()
    ->providers([
        Provider::make('github')
            ->label('Github')
            ->icon('fab-github')
            ->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 on the panel, for example:

use DutchCodingCompany\FilamentSocialite\FilamentSocialitePlugin;
use DutchCodingCompany\FilamentSocialite\Provider;

FilamentSocialitePlugin::make()
    ->providers([
        Provider::make('github')
            ->label('Github')
            ->icon('fab-github')
            ->with([
                // Add scopes here.
                // Add optional parameters here.
                'hd' => 'example.com',
            ]),
    ]),

Visibility

You can set the visibility of a provider, if it is not visible, buttons will not be rendered. All functionality will still be enabled.

use DutchCodingCompany\FilamentSocialite\FilamentSocialitePlugin;
use DutchCodingCompany\FilamentSocialite\Provider;

FilamentSocialitePlugin::make()
    ->providers([
        Provider::make('github')
            ->visible(fn () => true),
    ]),

Stateless Authentication

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.

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.

dutchcodingcompany/filament-socialite 适用场景与选型建议

dutchcodingcompany/filament-socialite 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.1M 次下载、GitHub Stars 达 214, 最近一次更新时间为 2022 年 04 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「dutchcodingcompany」 「filament-socialite」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 dutchcodingcompany/filament-socialite 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 dutchcodingcompany/filament-socialite 我们能提供哪些服务?
定制开发 / 二次开发

基于 dutchcodingcompany/filament-socialite 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.1M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 214
  • 点击次数: 34
  • 依赖项目数: 9
  • 推荐数: 0

GitHub 信息

  • Stars: 214
  • Watchers: 10
  • Forks: 56
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-04-08