PHP code example of happenv-com / filament-turnstile

1. Go to this page and download the library: Download happenv-com/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/ */

    

happenv-com / filament-turnstile example snippets


use Happenv\FilamentTurnstile\TurnstilePlugin;

$panel->plugin(TurnstilePlugin::make());

// config/filament-turnstile.php
return [
    'site_key' => env('TURNSTILE_SITE_KEY'),
    'secret_key' => env('TURNSTILE_SECRET_KEY'),
    'verify_url' => env('TURNSTILE_VERIFY_URL', 'https://challenges.cloudflare.com/turnstile/v0/siteverify'),
    'connect_timeout' => (int) env('TURNSTILE_CONNECT_TIMEOUT', 5),
    'timeout' => (int) env('TURNSTILE_TIMEOUT', 10),
    'theme' => env('TURNSTILE_THEME', 'auto'),
    'size' => env('TURNSTILE_SIZE', 'flexible'),
    'language' => env('TURNSTILE_LANGUAGE'),
    'reset_event' => env('TURNSTILE_RESET_EVENT', 'turnstile-reset'),
];

use Happenv\FilamentTurnstile\TurnstilePlugin;

public function panel(Panel $panel): Panel
{
    return $panel
        ->login()
        ->registration()
        ->passwordReset()
        ->plugin(
            TurnstilePlugin::make()
                ->protectRegistration()   // opt-in
                ->protectPasswordReset()  // opt-in (request page only)
        );
}

TurnstilePlugin::make()
    ->protectLogin(false)
    ->protectRegistration()
    ->protectPasswordReset()

TurnstilePlugin::make()
    ->theme('auto')      // auto | light | dark
    ->size('flexible')   // normal | flexible | compact
    ->language('en-US')
    ->enabled(true);

use Happenv\FilamentTurnstile\Forms\Components\Turnstile;

Turnstile::make('cf-turnstile-response')
    ->theme('dark')
    ->size('compact')
    ->turnstileAction('contact');

->login(CustomLogin::class)
->plugin(TurnstilePlugin::make()->protectLogin(false))

use Filament\Auth\Pages\Login as BaseLogin;
use Filament\Schemas\Schema;
use Happenv\FilamentTurnstile\Concerns\InteractsWithTurnstileLogin;

class Login extends BaseLogin
{
    use InteractsWithTurnstileLogin;

    // Optional — the trait provides a default form. When you override it,
    // keep the Turnstile component in the schema.
    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                $this->getEmailFormComponent(),
                $this->getPasswordFormComponent(),
                $this->getTurnstileFormComponent(),
            ]);
    }
}

->login(Login::class)
->plugin(TurnstilePlugin::make()->protectLogin(false))

use Happenv\FilamentTurnstile\Pages\Auth\Login;
use Livewire\Livewire;

// Asserts: Turnstile rejects the token and stops the action.
Livewire::test(Login::class)
    ->fillForm(['email' => '[email protected]', 'password' => 'password'])
    ->assertTurnstileBlocks('authenticate');

// Asserts: Turnstile accepts the token and does not stop the action
// (other validation errors are not its concern).
Livewire::test(Login::class)
    ->fillForm(['email' => '[email protected]', 'password' => 'password'])
    ->assertTurnstileAllows('authenticate');

->assertTurnstileAllows('submit', [$ticketId], schema: 'contactForm')

Livewire::test(Login::class)
    ->fillForm(['email' => '[email protected]', 'password' => 'password'])
    ->passTurnstile()
    ->call('authenticate')
    ->assertTurnstileInactive() // the challenge step has no widget
    ->fillForm(['app' => ['code' => $code]], 'multiFactorChallengeForm') // keyed by the provider id
    ->call('authenticate');

use Happenv\FilamentTurnstile\Testing\TestsTurnstile;

it('logs in through Turnstile', function () {
    config([
        'filament-turnstile.site_key' => TestsTurnstile::PASSING_SITE_KEY,
        'filament-turnstile.secret_key' => TestsTurnstile::PASSING_SECRET_KEY, // BLOCKING_SECRET_KEY to reject the token
    ]);

    visit('/admin/login')
        ->assertScript(
            "document.querySelector('.fi-fo-turnstile [name=\"cf-turnstile-response\"]')?.value",
            TestsTurnstile::DUMMY_TOKEN,
        )
        ->fill('[id="form.email"]', '[email protected]')
        ->fill('[id="form.password"]', 'password')
        ->submit()
        ->assertPathIs('/admin');
});
css
@source '../../../../vendor/happenv-com/filament-turnstile/resources/**/*.blade.php';
bash
php artisan vendor:publish --tag=filament-turnstile-config
bash
php artisan vendor:publish --tag=filament-turnstile-views
bash
php artisan vendor:publish --tag=filament-turnstile-translations