derekcodes-io/
turnstile-laravel
g4t.io

Cloudflare's Turnstile Captcha for Laravel

22 views 57 stars by Paul Redmond

The turnstile-laravel package integrates Turnstile's Captcha verification API with Laravel applications.

README from derekcodes-io/turnstile-laravel Open on GitHub →

Cloudflare's Turnstile For Laravel

A Laravel package to facilitate the server side validation of Cloudflare's Turnstile captcha service.

GitHub release GitHub repo size Packagist Downloads

Configuration

First you'll need an account Cloudflare and Turnstile setup for your website.

https://developers.cloudflare.com/turnstile/

Installation

Install via composer

composer require derekcodes/turnstile-laravel 

Adding your secret key in the .env file

TURNSTILE_SITE_KEY="0x4AAAAAAAXXXXXXXXXXXXXX"
TURNSTILE_SECRET_KEY="0x4AAAAAAAXXXXXXXXXXXXXX"

Create a config/turnstile.php

php artisan vendor:publish --tag=turnstile-config

Front-end

Be sure to add the front-end JavaScript from Turnstile: https://developers.cloudflare.com/turnstile/get-started/client-side-rendering/

To save you some time, here's the Turnstile JavaScript necessary for an HTML form. Note that I assume you're using a Blade template, thus the {{ config('turnstile.site_key') }}.

<script src="https://challenges.cloudflare.com/turnstile/v0/api.js?onload=onloadTurnstileCallback" defer></script>

window.onloadTurnstileCallback = function () {
    turnstile.render('#your-form-id', {
        sitekey: '{{ config('turnstile.site_key') }}',
        callback: function(token) {
            console.log(`Challenge Success ${token}`);
        },
    });
};

Usage

Add the use statement at the top of your file

use DerekCodes\TurnstileLaravel\TurnstileLaravel;

Initiate the TursileLaravel object and call the validate method passing the client response Turnstile's JavaScript provides

$turnstile = new TurnstileLaravel;
$response = $turnstile->validate($request->get('cf-turnstile-response'));

Ensure the response is valid

if (get_data($response, 'status', 0) == 1) {
  // TODO: add success code here
}

Responses

Example success response

{
  "status": 1
}

Example error response

{
  "status": 0,
  "turnstile_response": {
    "success": false,
    "error-codes": [
      "invalid-input-response"
    ],
    "messages": []
  }
}

Credits

~ Derek Codes

Derek Codes is a comprehensive collection of tutorials on the most popular languages in the industry. You’ll find videos, guides and downloadable assets to help you learn to code PHP, Laravel, JavaScript, CSS and more. Thanks for watching!

More from the ecosystem

durable-workflow/
workflow
g4t.io
9 1,292

Durable Workflow: Long-Running Workflows for Laravel

Embedded durable workflow runtime and orchestration engine for Laravel applications.

durable-workflow/workflow via Laravel News
packstub/
session-replay
g4t.io
14 52

Session Replay for Laravel: Self-Hosted Browser Recordings

Session replay for Laravel: record what a person did in the browser with rrweb, keep the recording on your own disk and database, and watch it behind a gate you define — masked inputs by default, errors, failed Livewire requests and web vitals marked on the timeline, a link to the replay in every log line. packstub/filament-session-replay adds the sessions to a Filament panel.

packstub/session-replay via Laravel News
cyberwizard-dev/
schedule-catchup
g4t.io
17

Replay Missed Laravel Scheduled Tasks With Schedule Catchup

Schedule Catchup replays missed Laravel scheduled tasks after downtime, with opt-in recovery, bounded retries, and access to the original scheduled date.

cyberwizard-dev/schedule-catchup via Laravel News