You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+103-8Lines changed: 103 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -184,7 +184,7 @@ When issuing a form, you have three alternatives to ensure the Turnstile challen
184
184
185
185
### Validating with Request
186
186
187
-
The easiest and less intrusive way to check the Turnstile challenge is to use the `Laragear\Turnstile\Http\Requests\TurnstileRequest` instance in your controller. This is great if you only have a few controllers where you want to check for a successful Turnstile Challenge.
187
+
The easiest and least intrusive way to check the Turnstile Challenge is to use the `Laragear\Turnstile\Http\Requests\TurnstileRequest` instance in your controller. This is great if you only have a few controllers where you want to stop bots.
188
188
189
189
```php
190
190
use App\Models\Comment;
@@ -200,7 +200,7 @@ Route::post('comment', function (TurnstileRequest $request) {
200
200
})
201
201
```
202
202
203
-
You can have access to the Cloudflare Turnstile Challenge object through the `challenge()` method. For example, you may use it to double-check if the action is equal to something you expect.
203
+
You can have access to the Cloudflare Turnstile Challenge object through the `challenge()` method, plus additional helpers for the Challenge instance itself. For example, you may use it to double-check if the action is equal to something you expect.
204
204
205
205
```php
206
206
use App\Models\Comment;
@@ -212,7 +212,7 @@ Route::post('comment', function (TurnstileRequest $request) {
212
212
'body' => 'required|string'
213
213
]);
214
214
215
-
if ($request->challenge()->isAction('comment:store')) {
215
+
if ($request->isAction('comment:store')) {
216
216
return back()->withErrors('Invalid action');
217
217
}
218
218
@@ -222,7 +222,87 @@ Route::post('comment', function (TurnstileRequest $request) {
222
222
223
223
> [!IMPORTANT]
224
224
>
225
-
> The Request will check for the `cf-turnstile-response` key [by default](#form-key), plus a successful Challenge. If you need more fine-tuning, consider using the [middleware](#validating-with-middleware), [rule](#validating-with-rule), or [validating manually](#validating-manually).
225
+
> The Request will check for the `cf-turnstile-response` key [by default](#form-key), plus a successful Challenge. If you need more fine-tuning, you may [extend the form request class](#extending-the-form-request). Alternatively, you may also use a [middleware](#validating-with-middleware), [rule](#validating-with-rule), or [validate manually](#validating-manually).
226
+
227
+
#### Extending the Form Request
228
+
229
+
If you need to create a form request and also validate the Turnstile Challenge, you may safely extend the `TurnstileRequest` instead of the base `FormRequest`. The class runs the validation _before_ your form request authorization and rules to avoid running side effects.
230
+
231
+
```php
232
+
namespace App\Http\Requests;
233
+
234
+
use Laragear\Turnstile\Http\Requests\TurnstileRequest;
235
+
236
+
class CommentStoreRequest extends TurnstileRequest
237
+
{
238
+
public function rules(): array
239
+
{
240
+
return [
241
+
'body' => 'required|string'
242
+
];
243
+
}
244
+
}
245
+
```
246
+
247
+
This means your controller can safely retrieve the validated data using `$request->validated()`, as the token won't be considered part of the Request itself.
248
+
249
+
```php
250
+
use App\Models\Comment;
251
+
use Illuminate\Support\Facades\Route;
252
+
use App\Http\Requests\CommentStoreRequest;
253
+
254
+
Route::post('comment', function (CommentStoreRequest $request) {
255
+
return Comment::create($request->validated());
256
+
})
257
+
```
258
+
259
+
#### Custom key and rules
260
+
261
+
You may also edit the key and the rules where to find and check the Response Token in the request. For the case of rules, ensure you're using [the `turnstile` rule](#validating-with-rule).
262
+
263
+
```php
264
+
namespace App\Http\Requests;
265
+
266
+
use Laragear\Turnstile\Http\Requests\TurnstileRequest;
267
+
268
+
class CommentStoreRequest extends TurnstileRequest
269
+
{
270
+
public function getTurnstileKey()
271
+
{
272
+
return '_cf-custom-key';
273
+
}
274
+
275
+
protected function getTurnstileRules()
276
+
{
277
+
// Skip if the user is authenticated.
278
+
return 'turnstile:auth'
279
+
}
280
+
281
+
// ...
282
+
}
283
+
```
284
+
285
+
#### Precognitive request
286
+
287
+
The `TurnstileRequest` won't check for the Challenge Token on [Precognitive Requests](https://laravel.com/docs/12.x/precognition), which is useful to not disrupt [live-validation](https://laravel.com/docs/12.x/precognition#live-validation).
288
+
289
+
If you require custom validation on precognitive requests, you may override the `skipChallengeWhenPrecognitive()` method.
290
+
291
+
```php
292
+
namespace App\Http\Requests;
293
+
294
+
use Laragear\Turnstile\Http\Requests\TurnstileRequest;
295
+
296
+
class CommentStoreRequest extends TurnstileRequest
297
+
{
298
+
protected function skipChallengeWhenPrecognitive(): bool
By default, the middleware will [skip running on Precognitive requests](https://laravel.com/docs/12.x/precognition#managing-side-effects). If you want to run it, set the `TurnstileMiddleware::onPrecognitive()` option, especially if your validation has side effects.
452
+
453
+
```php
454
+
use Illuminate\Http\Request;
455
+
use Illuminate\Support\Facades\Route;
456
+
use Laragear\Turnstile\Http\Middleware\TurnstileMiddleware;
457
+
458
+
Route::post('comment', function (Request $request) {
You can use the `turnstile` rule to check if the Turnstile challenge is present and is successful. The easiest way is to unpack the default rule contained in the `rules()` method of the `Turnstile` facade.
466
+
You can use the `turnstile` rule to check if the Turnstile challenge is present and is successful in the data to validate. The easiest way is to unpack the default rule contained in the `rules()` method of the `Turnstile` facade.
372
467
373
468
```php
374
469
use Illuminate\Http\Request;
@@ -454,9 +549,9 @@ public function create(Request $request)
454
549
455
550
> [!IMPORTANT]
456
551
>
457
-
> The challenge is automatically retrieved by the [request](#validating-with-request), [middleware](#validating-with-middleware) and [rule](#validating-with-rule). If that's case, you may [use the `challenge()` method](#retrieving-an-already-received-challenge).
552
+
> The challenge is automatically retrieved by the [request](#validating-with-request), [middleware](#validating-with-middleware) and [rule](#validating-with-rule). If that's case, you may [use the `challenge()` method](#retrieving-an-already-received-challenge) instead.
458
553
459
-
To validate the Challenge manually, first you require the Turnstile Response Token that is sent by the frontend, and optionally the IP of the Request.
554
+
To validate the Challenge manually, you require the Turnstile Response Token that is sent by the frontend, and optionally the IP of the Request.
460
555
461
556
Once identified, you should use the `getChallenge()` method of `Turnstile` facade to retrieve the Challenge from Cloudflare Turnstile servers.
0 commit comments