Skip to content

Commit 9bad148

Browse files
authored
Merge pull request #12 from coderflexx/reset-captcha-event
Reset Captcha Event
2 parents 2c8f78f + ea86a4c commit 9bad148

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed

README.md

+70-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
</br>
99

10-
![Login Screen screenshot](https://github.com/coderflexx/filament-turnstile/raw/main/art/thumbnail.png)
10+
<img src="https://github.com/coderflexx/filament-turnstile/raw/main/art/thumbnail.png" alt="Login Screen screenshot" class="filament-hidden"/>
1111

1212
</br>
1313

@@ -59,6 +59,67 @@ For a list of supported languages, refer to the [supported languages section](ht
5959

6060
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).
6161

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.
62123

63124
## Real-Life Example:
64125

@@ -97,6 +158,14 @@ class Login extends AuthLogin
97158
),
98159
];
99160
}
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+
}
100169
}
101170
```
102171

resources/views/components/turnstile.blade.php

+14
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
window.onloadTurnstileCallback = () => {
2828
turnstile.render($refs.turnstile, options)
2929
}
30+
31+
resetCaptcha = () => {
32+
turnstile.reset($refs.turnstile)
33+
}
3034
})()"
3135
>
3236
<div data-sitekey="{{config('turnstile.turnstile_site_key')}}"
@@ -39,4 +43,14 @@
3943
</div>
4044

4145
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js?onload=onloadTurnstileCallback" defer></script>
46+
47+
@push('scripts')
48+
<script>
49+
document.addEventListener('livewire:init', () => {
50+
Livewire.on('reset-captcha', (event) => {
51+
resetCaptcha()
52+
})
53+
})
54+
</script>
55+
@endpush
4256
</x-dynamic-component>

tests/Fixtures/ContactUs.php

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Filament\Forms;
88
use Filament\Forms\Form;
99
use Filament\Forms\FormsComponent;
10+
use Illuminate\Validation\ValidationException;
1011

1112
class ContactUs extends FormsComponent
1213
{
@@ -55,4 +56,9 @@ public function render()
5556
{
5657
return 'fixtures.contact-us';
5758
}
59+
60+
protected function onValidationError(ValidationException $exception): void
61+
{
62+
$this->dispatch('reset-captcha');
63+
}
5864
}

tests/TurnstileTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Coderflex\FilamentTurnstile\Tests\Models\Contact;
55
use Coderflex\LaravelTurnstile\Facades\LaravelTurnstile;
66
use Illuminate\Support\Facades\Config;
7+
use Illuminate\Support\Facades\Event;
78

89
use function Pest\Livewire\livewire;
910

@@ -94,3 +95,22 @@
9495
expect(Contact::get())
9596
->toHaveCount(0);
9697
});
98+
99+
it('reset captcha event sent, on validation error ', function () {
100+
Event::fake();
101+
102+
Config::set('turnstile', [
103+
'turnstile_site_key' => '2x00000000000000000000AB',
104+
'turnstile_secret_key' => '2x0000000000000000000000000000000AA',
105+
]);
106+
107+
livewire(ContactUs::class)
108+
->fillForm([
109+
'name' => null,
110+
'email' => '[email protected]',
111+
'content' => 'This is a simple message',
112+
])
113+
->call('send')
114+
->assertHasFormErrors(['name'])
115+
->assertDispatched('reset-captcha');
116+
});

0 commit comments

Comments
 (0)