|
7 | 7 |
|
8 | 8 | </br>
|
9 | 9 |
|
10 |
| - |
| 10 | +<img src="https://github.com/coderflexx/filament-turnstile/raw/main/art/thumbnail.png" alt="Login Screen screenshot" class="filament-hidden"/> |
11 | 11 |
|
12 | 12 | </br>
|
13 | 13 |
|
@@ -59,6 +59,67 @@ For a list of supported languages, refer to the [supported languages section](ht
|
59 | 59 |
|
60 | 60 | The `Turnstile` field offers various options; you can learn more about them in [the Cloudflare configuration section](https://developers.cloudflare.com/turnstile/get-started/client-side-rendering/#configurations).
|
61 | 61 |
|
| 62 | +## Turnstile Events |
| 63 | + |
| 64 | +The Turnstile package provides events that you can leverage to manage the behavior of the captcha in various scenarios. |
| 65 | + |
| 66 | +**Reset Event** |
| 67 | + |
| 68 | +The `reset-captcha` event allows you to programmatically reset the captcha challenge. This can be useful when you want to: |
| 69 | + |
| 70 | +- **Clear the challenge after a successful form submission:** This ensures a fresh captcha for the next user. |
| 71 | +- **Reset the challenge upon validation errors:** Prevents users from being stuck with a previously solved captcha after encountering errors during form submission. |
| 72 | + |
| 73 | +**Dispatching the Reset Event:** |
| 74 | + |
| 75 | +There are two primary ways to dispatch the `reset-captcha` event: |
| 76 | + |
| 77 | +**1. Using `onValidationError` Method:** |
| 78 | + |
| 79 | +Filament provides the `onValidationError` method within your form's Livewire component. This method is automatically triggered whenever form [validation fails](https://filamentphp.com/docs/3.x/forms/validation#sending-validation-notifications). Here's how to utilize it: |
| 80 | + |
| 81 | +```php |
| 82 | +protected function onValidationError(ValidationException $exception): void |
| 83 | +{ |
| 84 | + $this->dispatch('reset-captcha'); |
| 85 | + |
| 86 | + // Perform additional actions as necessary (e.g., display error messages) |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +In this example, the `reset-captcha` event is dispatched upon validation errors, ensuring the captcha is reset for the user's next attempt. |
| 91 | + |
| 92 | +**2. Manual Dispatching:** |
| 93 | + |
| 94 | +For scenarios where resetting the captcha is not directly tied to validation, you can manually dispatch the event using Filament's event dispatcher: |
| 95 | + |
| 96 | +```php |
| 97 | +$this->dispatch('reset-captcha'); |
| 98 | +``` |
| 99 | + |
| 100 | +**Using Reset Event in Login Page:** |
| 101 | + |
| 102 | +To automatically reset the captcha on a failed login attempt in your login form's Livewire component, leverage the `throwFailureValidationException` method: |
| 103 | + |
| 104 | +```php |
| 105 | +protected function authenticate(): void |
| 106 | +{ |
| 107 | + // Perform authentication logic |
| 108 | + // ... |
| 109 | + |
| 110 | + if (! Auth::attempt($this->data)) { |
| 111 | + $this->throwFailureValidationException( |
| 112 | + [ |
| 113 | + 'email' => 'Invalid email or password.', |
| 114 | + ] |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + // Redirect to success page or perform other actions |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +By throwing a validation exception with appropriate error messages, you trigger the `onValidationError` method, which in turn dispatches the `reset-captcha` event, effectively resetting the captcha for the next login attempt. |
62 | 123 |
|
63 | 124 | ## Real-Life Example:
|
64 | 125 |
|
@@ -97,6 +158,14 @@ class Login extends AuthLogin
|
97 | 158 | ),
|
98 | 159 | ];
|
99 | 160 | }
|
| 161 | + |
| 162 | + // if you want to reset the captcha in case of validation error |
| 163 | + protected function throwFailureValidationException(): never |
| 164 | + { |
| 165 | + $this->dispatch('reset-captcha'); |
| 166 | + |
| 167 | + parent::throwFailureValidationException(); |
| 168 | + } |
100 | 169 | }
|
101 | 170 | ```
|
102 | 171 |
|
|
0 commit comments