The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

Laravel Cloudflare Turnstile

ryangjchandler/laravel-cloudflare-turnstile image

Laravel Cloudflare Turnstile stats

Downloads
19.9K
Stars
99
Open Issues
5
Forks
4

View on GitHub →

A simple package to help integrate Cloudflare Turnstile.

A simple package to help integrate Cloudflare Turnstile.

This packages provides helper for setting up and validating Cloudflare Turnstile CAPTCHA responses.

Installation

You can install the package via Composer:

composer require ryangjchandler/laravel-cloudflare-turnstile

You should then add the following configuration values to your config/services.php file:

return [
 
// ...,
 
'turnstile' => [
'key' => env('TURNSTILE_SITE_KEY'),
'secret' => env('TURNSTILE_SECRET_KEY'),
],
 
];

Visit Cloudflare to create your site key and secret key and add them to your .env file.

TURNSTILE_SITE_KEY="1x00000000000000000000AA"
TURNSTILE_SECRET_KEY="2x0000000000000000000000000000000AA"

Usage

In your layout file, include the Turnstile scripts using the @turnstileScripts Blade directive. This should be added to the <head> of your document.

<html>
<head>
@turnstileScripts()
</head>
<body>
{{ $slot }}
</body>
</html>

Once that's done, you can use the <x-turnstile /> component inside of a <form> to output the appropriate markup with your site key configured.

<form action="/" method="POST">
<x-turnstile />
 
<button>
Submit
</button>
</form>

On the server, use the provided validation rule to validate the CAPTCHA response.

use Illuminate\Validation\Rule;
 
public function submit(Request $request)
{
$request->validate([
'cf-turnstile-response' => ['required', Rule::turnstile()],
]);
}

If you prefer to not use a macro, you can resolve an instance of the rule from the container via dependency injection or the app() helper.

use RyanChandler\LaravelCloudflareTurnstile\Rules\Turnstile;
 
public function submit(Request $request, Turnstile $turnstile)
{
$request->validate([
'cf-turnstile-response' => ['required', $turnstile],
]);
}
use RyanChandler\LaravelCloudflareTurnstile\Rules\Turnstile;
 
public function submit(Request $request)
{
$request->validate([
'cf-turnstile-response' => ['required', app(Turnstile::class)],
]);
}

Customizing the widget

You can customize the widget by passing attributes to the <x-turnstile /> component.

To learn more about these parameters, refer to the offical documentation.

<form action="/" method="POST">
<x-turnstile
data-action="login"
data-cdata="sessionid-123456789"
data-callback="callback"
data-expired-callback="expiredCallback"
data-error-callback="errorCallback"
data-theme="dark"
data-tabindex="1"
/>
 
<button>
Submit
</button>
</form>

This package can also integrate seamlessly with Livewire. Upon successful validation, the property specified inside of wire:model will be updated with the Turnstile token.

<x-turnstile wire:model="yourModel" />

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.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Ryangjchandler Laravel Cloudflare Turnstile Related Articles

A Flat-File Database Driver for Eloquent image

A Flat-File Database Driver for Eloquent

Read article
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
DreamzTech logo

DreamzTech

Hire 6-10+ Yrs. experienced skilled Laravel Developers from DreamzTech. We ensure NDA protected, 100% quality delivery. Contact Us & Discuss Your Need.

DreamzTech
Celebian logo

Celebian

Celebian is a social media marketing agency specializing in helping their clients go viral on TikTok. Whether you're looking to reach a bigger audience or gain more Tiktok followers, likes, and views, they've got you covered.

Celebian
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Typesense Search logo

Typesense Search

Typesense is an open source, blazing-fast search engine, optimized for helping you build delightful search experiences for your sites and apps. Natively integrated with Laravel Scout.

Typesense Search