altrntv / filament-recaptcha
Google reCAPTCHA for Filament v4 and v5
Requires
- php: ^8.2
- filament/filament: ^4.0 || ^5.0
- google/cloud-recaptcha-enterprise: ^2.6
- illuminate/contracts: ^11.0 || ^12.0 || ^13.0
- illuminate/http: ^11.0 || ^12.0 || ^13.0
- illuminate/support: ^11.0 || ^12.0 || ^13.0
- spatie/laravel-package-tools: ^1.15.0
Requires (Dev)
- driftingly/rector-laravel: ^2.1
- ergebnis/composer-normalize: ^2.59
- larastan/larastan: ^3.0
- laravel/pint: ^1.0
- nunomaduro/collision: ^8.0
- orchestra/testbench: ^10.0 || ^11.0
- pestphp/pest: ^4.0 || ^5.0
- pestphp/pest-plugin-arch: ^4.0 || ^5.0
- pestphp/pest-plugin-browser: ^4.0 || ^5.0
- pestphp/pest-plugin-laravel: ^4.0 || ^5.0
- pestphp/pest-plugin-livewire: ^4.0 || ^5.0
- rector/rector: ^2.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Add a Google reCAPTCHA Enterprise checkbox to Filament login, registration, and password reset request forms. The server verifies each token using the Enterprise Assessments API. The login page supports Filament's multi-factor authentication without submitting the same reCAPTCHA token twice.
Installation
The package supports Filament 4 and 5.
composer require altrntv/filament-recaptcha
In your Google Cloud project, enable the reCAPTCHA Enterprise API, create a checkbox site key for your application's domains, and create an API key in the same project for server-side assessments. Create a checkbox site key ยท Authenticate assessment requests with an API key
Add your project ID and both keys to your application's .env file. Keep the API key on the server.
RECAPTCHA_PROJECT_ID=your-google-cloud-project-id RECAPTCHA_SITE_KEY=your-recaptcha-site-key RECAPTCHA_API_KEY=your-google-cloud-api-key
Panel setup
Register the plugin in your panel provider after configuring the authentication pages. Login protection is enabled by default. Opt into registration and password reset request protection with the corresponding plugin methods.
use Altrntv\FilamentRecaptcha\RecaptchaPlugin; use Filament\Panel; public function panel(Panel $panel): Panel { return $panel ->id('admin') ->path('admin') ->login() ->registration() ->passwordReset() ->plugin( RecaptchaPlugin::make() ->protectRegistration() ->protectPasswordReset(), ); }
protectPasswordReset() protects the form that requests a reset link. If you leave both the project ID and API key unset, reCAPTCHA is inactive. Once either is set, all three credentials are required for verification.
Widget options
| Environment variable | Default | Purpose |
|---|---|---|
RECAPTCHA_THEME |
auto |
Follows Filament's theme. Use light or dark to fix the widget theme. |
RECAPTCHA_LANGUAGE |
Unset | Lets Google detect the language by default. Set a language code such as en or fr to override it. |
RECAPTCHA_RESET_EVENT |
recaptcha-reset |
Livewire event that resets the widget and clears its token. |
Set panel-level overrides with RecaptchaPlugin::make()->theme('dark')->language('en'). A Recaptcha field can override those values with its own theme() and language() methods and listen for a custom event via resetEvent().
Publish the configuration or Blade view if you need to customize them.
php artisan vendor:publish --tag=filament-recaptcha-config php artisan vendor:publish --tag=filament-recaptcha-views
Custom authentication pages
Use the matching trait on your custom Filament page.
| Filament page | Trait in Altrntv\FilamentRecaptcha\Concerns |
|---|---|
Login |
InteractsWithRecaptchaLogin |
Register |
InteractsWithRecaptchaRegistration |
RequestPasswordReset |
InteractsWithRecaptchaPasswordReset |
If you override form(), include $this->getRecaptchaFormComponent() in the schema. Disable automatic replacement for your custom page when registering the plugin. For example, call ->login(CustomLogin::class) on the panel and ->protectLogin(false) on the plugin.
For other Filament forms, add Altrntv\FilamentRecaptcha\Forms\Components\Recaptcha::make('g-recaptcha-response') to the schema. The field validates the token during form validation and omits it from the submitted form state.
Testing
The package registers Livewire test helpers that fake Enterprise assessment responses. Set any other required fields before using them.
use Altrntv\FilamentRecaptcha\Pages\Auth\Login; use Livewire\Livewire; Livewire::test(Login::class) ->set('data.email', 'user@example.com') ->set('data.password', 'password') ->assertRecaptchaBlocks('authenticate');
Use assertRecaptchaAllows($action) to check that reCAPTCHA permits an action. Use passRecaptcha() or failRecaptcha() to set the token before calling the action yourself. Use assertRecaptchaActive() or assertRecaptchaInactive() to check whether the form has an active widget.
To run this package's unit and feature tests from a checkout, install the development dependencies with Composer and run:
composer test -- --testsuite=Unit,Feature
For browser tests, install Playwright's Chromium. The browser suite uses local reCAPTCHA test doubles and does not require Google credentials.
npm install
npx playwright install chromium
composer test -- --testsuite=Browser
Upgrading
Breaking changes and how to migrate are described in UPGRADING for every major version.
Changelog
See CHANGELOG and GitHub releases for what has changed recently.
Contributing
See CONTRIBUTING for details.
Security vulnerabilities
Please review our security policy on how to report security vulnerabilities.
License
The MIT License (MIT). See License File for more information.