mix-code/filament-multi-2fa 问题修复 & 功能扩展

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

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

mix-code/filament-multi-2fa

Composer 安装命令:

composer require mix-code/filament-multi-2fa

包简介

Implementing Email OTP and Authenticator App 2FA Logic with Trusted Devices Support

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Implementing Email OTP and Authenticator App 2FA logic with Trusted Devices support.

Features

Installation

You can install the package via composer:

composer require mix-code/filament-multi-2fa

Install plugin configs and migrations:

php artisan filament-multi-2fa:install

Optionally, you can publish the lang files with:

php artisan vendor:publish --tag="filament-multi-2fa-translations"

Optionally, you can publish the views using:

php artisan vendor:publish --tag="filament-multi-2fa-views"

Usage

1️⃣ Register the plugin in your Filament Panel

In your PanelProvider (e.g., AdminPanelProvider):

public function panel(Panel $panel): Panel
{
    return $panel
        // other panel setup...
        ->plugins([
            // ...
            \MixCode\FilamentMulti2fa\FilamentMulti2faPlugin::make(),
        ]);
}

Force 2FA setup:

public function panel(Panel $panel): Panel
{
    return $panel
        ->plugins([
            \MixCode\FilamentMulti2fa\FilamentMulti2faPlugin::make()
                ->forceSetup2fa(),
        ]);
}

2️⃣ Configure User Model

In your User model, use the UsingTwoFA trait, guarded attributes and casts:

use MixCode\FilamentMulti2fa\Traits\UsingTwoFA;
use MixCode\FilamentMulti2fa\Enums\TwoFactorAuthType;

class User extends Authenticatable
{
    use UsingTwoFA;

    protected $guarded = [
        'two_factor_type',
        'two_factor_secret',
        'two_factor_recovery_codes',
        'two_factor_sent_at',
        'two_factor_expires_at',
        'two_factor_confirmed_at',
    ];

    protected $casts = [
        'two_factor_type' => TwoFactorAuthType::class,
        'two_factor_sent_at' => 'datetime',
        'two_factor_expires_at' => 'datetime',
        'two_factor_confirmed_at' => 'datetime',
    ];

    // ...
}

You can also configure redirection after OTP verification by overriding redirectAfterVerifyUrl:

public function redirectAfterVerifyUrl(): ?string
{
    return route('home');
}

To list user trusted devices:

$user->trustedDevices();

3️⃣ Features Automatically Handled by the Plugin:

  • 🛡️ 2FA Setup Page (users can select Email OTP or Authenticator App)
  • 🔑 OTP Verification Page to protect access based on trusted device and 2FA status
  • 🖥 Trusted Device Middleware to check trusted device cookies and enforce OTP verification
  • 🔐 Adds a shortcut in the user menu to access the 2FA setup page

4️⃣ Automatic Logout Handling

When users log out, the plugin clears their two_factor_confirmed_at column automatically:

Event::listen(Logout::class, function ($event) {
    $user = $event->user;

    if ($user) {
        $user->two_factor_confirmed_at = null;
        $user->save();
    }
});

5️⃣ Customize settings from the config file

return [

    /*
    |--------------------------------------------------------------------------
    | Models Configuration
    |--------------------------------------------------------------------------
    |
    | Define the models used by the package here. These models will be used
    | for users and trusted device storage.
    |
    */

    'user_model' => \App\Models\User::class,

    'trust_device_model' => \MixCode\FilamentMulti2fa\Models\TrustDevice::class,

    /*
    |--------------------------------------------------------------------------
    | Notifications
    |--------------------------------------------------------------------------
    |
    | Specify the notification class responsible for sending the OTP code
    | to the user.
    |
    */

    'otp_notification_class' => \MixCode\FilamentMulti2fa\Notifications\TwoFactorCodeNotification::class,

    /*
    |--------------------------------------------------------------------------
    | QR Code Rendering Backend
    |--------------------------------------------------------------------------
    |
    | Choose the QR code rendering service to generate the QR Code for TOTP.
    |
    | Supported Services:
    |
    | 1. BaconQrCode (default):
    |    - Renders PNG (inline or file)
    |    - Requires the Imagick PHP extension (depending on backend)
    |    - Can render SVG with custom setup, but PNG is common default
    |
    | 2. chillerlan/php-qrcode:
    |    - Outputs base64-encoded PNG by default
    |    - Supports SVG rendering via OUTPUT_MARKUP_SVG
    |    - Does NOT require Imagick or GD by default for PNG base64
    |    - Other formats (PNG file, SVG file) may require GD/Imagick if saved to disk
    |
    */

    // Default to BaconQrCode
    'qr_code_backend_service' => \PragmaRX\Google2FAQRCode\QRCode\Bacon::class,

    // To use chillerlan/php-qrcode (SVG by default), uncomment below:
    // 'qr_code_backend_service' => \PragmaRX\Google2FAQRCode\QRCode\Chillerlan::class,

    /*
    |--------------------------------------------------------------------------
    | OTP Settings
    |--------------------------------------------------------------------------
    |
    | OTP Specific Settings From the view of the notification to the resend remains.
    |
    */

    'otp_view' => 'filament-multi-2fa::emails.2fa.otp',

    'otp_resend_time_format' => '%i:%S', // 1:15

    'otp_resend_allowed_after_in_seconds' => 60 * 1, // 1 Minute

    'otp_expiration_in_seconds' => 60 * 10,   // 10 Minute

    /*
    |--------------------------------------------------------------------------
    | Trusted Device Settings
    |--------------------------------------------------------------------------
    |
    | Configure trusted device cookie settings, including cookie name,
    | lifespan in minutes, and cache settings to minimize DB hits.
    |
    */

    'trusted_device_cookie_name' => 'trusted_device',

    // Duration (in minutes) before a trusted device expires in the database
    'trusted_device_db_expiration' => 60 * 24 * 30, // 30 days

    // Duration (in minutes) before a trusted device cookie expires
    'trusted_device_cookie_lifespan' => 60 * 24 * 30, // 30 days

    // Duration (in minutes) to cache trusted device checks to reduce DB queries
    'trust_device_check_cache_lifespan' => 60 * 24 * 30, // 30 days

];

📝 Notes:

  • The plugin automatically injects the CheckTrustedDevice middleware to protect your Filament admin routes.
  • After OTP verification, users are redirected to the current panel’s dashboard.

Testing

composer test

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.

mix-code/filament-multi-2fa 适用场景与选型建议

mix-code/filament-multi-2fa 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.99k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2025 年 03 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 mix-code/filament-multi-2fa 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 5.99k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 27
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 2
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-20