coderflex/filament-turnstile
Composer 安装命令:
composer require coderflex/filament-turnstile
包简介
Filament Plugin to help you implement Cloudflare Turnstile
README 文档
README
Filament Turnstile is an essential plugin designed to seamlessly integrate Cloudflare's turnstile into your applications.
This plugin uses Laravel Turnstile under the hood. For detailed information, explore the Laravel Turnstile README.
Installation
Install the package via Composer:
composer require coderflex/filament-turnstile
For users still on Filament V2, install the package using:
composer require coderflex/filament-turnstil "^1.0"
Turnstile Keys
To utilize Cloudflare Turnstile, obtain your SiteKey and SecretKey from your Cloudflare Dashboard.
Refer to the documentation for detailed instructions.
After generating the keys, include them in your .env file using the following format:
TURNSTILE_SITE_KEY=1x00000000000000000000AA TURNSTILE_SECRET_KEY=1x0000000000000000000000000000000AA
For testing purposes, you can use Dummy site keys and secret keys provided by Cloudflare.
Usage
Utilizing this plugin is incredibly straightforward. In your form, incorporate the following code:
use Coderflex\FilamentTurnstile\Forms\Components\Turnstile; Turnstile::make('captcha') ->theme('auto') // accepts light, dark, auto ->language('en-US') // see below ->size('normal'), // accepts normal, compact
For a list of supported languages, refer to the supported languages section.
The Turnstile field offers various options; you can learn more about them in the Cloudflare configuration section.
Turnstile Events
The Turnstile package provides events that you can leverage to manage the behavior of the captcha in various scenarios.
Reset Event
The reset-captcha event allows you to programmatically reset the captcha challenge. This can be useful when you want to:
- Clear the challenge after a successful form submission: This ensures a fresh captcha for the next user.
- Reset the challenge upon validation errors: Prevents users from being stuck with a previously solved captcha after encountering errors during form submission.
Dispatching the Reset Event:
There are two primary ways to dispatch the reset-captcha event:
1. Using onValidationError Method:
Filament provides the onValidationError method within your form's Livewire component. This method is automatically triggered whenever form validation fails. Here's how to utilize it:
protected function onValidationError(ValidationException $exception): void { $this->dispatch('reset-captcha'); // Perform additional actions as necessary (e.g., display error messages) }
In this example, the reset-captcha event is dispatched upon validation errors, ensuring the captcha is reset for the user's next attempt.
2. Manual Dispatching:
For scenarios where resetting the captcha is not directly tied to validation, you can manually dispatch the event using Filament's event dispatcher:
$this->dispatch('reset-captcha');
Using Reset Event in Login Page:
To automatically reset the captcha on a failed login attempt in your login form's Livewire component, leverage the throwFailureValidationException method:
protected function authenticate(): void { // Perform authentication logic // ... if (! Auth::attempt($this->data)) { $this->throwFailureValidationException( [ 'email' => 'Invalid email or password.', ] ); } // Redirect to success page or perform other actions }
By throwing a validation exception with appropriate error messages, you trigger the onValidationError method, which in turn dispatches the reset-captcha event, effectively resetting the captcha for the next login attempt.
Real-Life Example:
To implement the Turnstile captcha with the Login page in Filament, follow these steps:
Create a new App/Filament/Pages/Auth/Login.php class:
namespace App\Filament\Pages\Auth; use Coderflex\FilamentTurnstile\Forms\Components\Turnstile; use Filament\Forms\Form; use Filament\Http\Responses\Auth\Contracts\LoginResponse; use Filament\Pages\Auth\Login as AuthLogin; class Login extends AuthLogin { /** * @return array<int|string, string|Form> */ protected function getForms(): array { return [ 'form' => $this->form( $this->makeForm() ->schema([ $this->getEmailFormComponent(), $this->getPasswordFormComponent(), $this->getRememberFormComponent(), Turnstile::make('captcha') ->label('Captcha') ->theme('auto'), ]) ->statePath('data'), ), ]; } // if you want to reset the captcha in case of validation error protected function throwFailureValidationException(): never { $this->dispatch('reset-captcha'); parent::throwFailureValidationException(); } }
Then, override the login() method in your PanelProvider (e.g., AdminPanelProvider):
namespace App\Providers\Filament; use App\Filament\Auth\Login; use Filament\Panel; use Filament\PanelProvider; class AdminPanelProvider extends PanelProvider { public function panel(Panel $panel): Panel { return $panel ->default() ->id('admin') ->path('admin') ->login(Login::class); // override the login page class. ... } }
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 the License File for more information.
coderflex/filament-turnstile 适用场景与选型建议
coderflex/filament-turnstile 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 137.08k 次下载、GitHub Stars 达 30, 最近一次更新时间为 2023 年 05 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「cloudflare」 「filament」 「coderflex」 「turnstile」 「laravel-turnstile」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 coderflex/filament-turnstile 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 coderflex/filament-turnstile 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 coderflex/filament-turnstile 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Symfony bundle for Cloudflare integration: trusted proxies, real client IP detection, forwarded headers.
Cloudflare Middleware For Guzzle
Library for helping bypass the Cloudflare IUAM page
A ZF2 plugin to fix global variables when using CloudFlare
Boost the performance of your site with Cloudflare or Varnish.
Alfabank REST API integration
统计信息
- 总下载量: 137.08k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 30
- 点击次数: 35
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-05-31