1. Go to this page and download the library: Download l3aro/filament-turnstile library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use l3aro\FilamentTurnstile\Enums\TurnstileSize;
use l3aro\FilamentTurnstile\Enums\TurnstileTheme;
use l3aro\FilamentTurnstile\Forms\Turnstile;
Turnstile::make('captcha')
->theme(TurnstileTheme::Auto)
->size(TurnstileSize::Normal)
->language('en-US')
->resetEvent('reset-captcha')
use l3aro\FilamentTurnstile\Facades\FilamentTurnstileFacade;
protected function onValidationError(ValidationException $exception): void
{
$this->dispatch(FilamentTurnstileFacade::getResetEventName());
// Perform additional actions as needed (e.g., display error messages)
}
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
}
namespace App\Filament\Pages\Auth;
use l3aro\FilamentTurnstile\Forms\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'),
])
->statePath('data'),
),
];
}
// if you want to reset the captcha in case of validation error
protected function throwFailureValidationException(): never
{
$this->dispatch(FilamentTurnstileFacade::getResetEventName());
parent::throwFailureValidationException();
}
}
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.
...
}
}