|
1 |
| -# Installation |
| 1 | +# Livewire ReCAPTCHA v3 |
| 2 | +[](https://packagist.org/packages/dutchcodingcompany/livewire-recaptcha) |
| 3 | +[](https://github.com/dutchcodingcompany/livewire-recaptcha/actions?query=workflow%3Arun-tests+branch%3Amain) |
| 4 | +[](https://github.com/dutchcodingcompany/livewire-recaptcha/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain) |
| 5 | +[](https://packagist.org/packages/dutchcodingcompany/livewire-recaptcha) |
| 6 | + |
| 7 | +This package provides a custom Livewire directive to protect your Livewire functions with a _Google reCAPTCHA v3_ check. |
| 8 | + |
| 9 | +## Installation |
2 | 10 | ```shell
|
3 | 11 | composer require dutchcodingcompany/livewire-recaptcha
|
4 | 12 | ```
|
5 | 13 |
|
6 |
| -# Configuration |
7 |
| -### View |
8 |
| -Include the required Google reCAPTCHA script and the custom Livewire directive by |
9 |
| -using the Blade directive `@livewireRecaptcha` in your view. This will add two `<script>` tags. |
10 |
| - |
11 |
| -Preferred method: only include it on the page where you have a reCAPTCHA protected form: |
12 |
| -```html |
13 |
| -<!-- someform.blade.php --> |
| 14 | +## Configuration |
| 15 | +Next, read https://developers.google.com/recaptcha/docs/v3 on how to create your own key pair. |
14 | 16 |
|
15 |
| -<!-- Add the `wire:recaptcha` directive --> |
16 |
| -<form wire:submit="save" wire:recaptcha> |
17 |
| - <!-- The rest of your form --> |
18 |
| -</form> |
| 17 | +The site key and secret key should be defined in your `config/services.php` file: |
19 | 18 |
|
20 |
| -@livewireRecaptcha |
21 |
| -<!-- or push it to some stack using @push('...')@endpush if you want to control it manually --> |
| 19 | +```php |
| 20 | +// ... |
| 21 | +'google' => [ |
| 22 | + 'recaptcha' => [ |
| 23 | + 'site_key' => env('GOOGLE_RECAPTCHA_SITE_KEY'), |
| 24 | + 'secret_key' => env('GOOGLE_RECAPTCHA_SECRET_KEY'), |
| 25 | + ] |
| 26 | +], |
22 | 27 | ```
|
| 28 | +#### Component |
| 29 | +In your Livewire component, at your form submission method, add the `#[ValidatesRecaptcha]` attribute: |
23 | 30 |
|
24 |
| -Alternatively, you can add the `@livewireRecaptcha` directive on a higher level, lets say your layout. |
25 |
| - |
26 |
| -### Component |
27 |
| -At your form submission method in your component, add the following attribute: |
28 | 31 | ```php
|
29 |
| - // SomeComponent.php |
| 32 | +use Livewire\Component; |
| 33 | + |
| 34 | +class SomeComponent extends Component |
| 35 | +{ |
30 | 36 | use DutchCodingCompany\LivewireRecaptcha\ValidatesRecaptcha;
|
31 | 37 |
|
32 | 38 | // (optional) Set a response property on your component.
|
33 |
| - // If not given, the property is dynamically assigned. |
| 39 | + // If not given, the `gRecaptchaResponse` property is dynamically assigned. |
34 | 40 | public string $gRecaptchaResponse;
|
35 | 41 |
|
36 |
| - /* |
37 |
| - * (default) take keys from: |
38 |
| - * config('services.google.recaptcha.site_key') |
39 |
| - * config('services.google.recaptcha.secret_key') |
40 |
| - */ |
41 | 42 | #[ValidatesRecaptcha]
|
42 |
| - /* |
43 |
| - * (alternative) manually provide keys: |
44 |
| - */ |
45 |
| - #[ValidatesRecaptcha(siteKey: 'foo', secretKey: 'bar')] |
46 | 43 | public function save(): mixed
|
47 | 44 | {
|
48 | 45 | // Your logic here will only be called if the captcha passes...
|
49 | 46 | }
|
| 47 | +} |
| 48 | +``` |
| 49 | +For fine-grained control, you can pass manual site and secret keys using: |
| 50 | +```php |
| 51 | +#[ValidatesRecaptcha(siteKey: 'mysitekey', secretKey: 'mysecretkey')] |
| 52 | +``` |
| 53 | +If you do not pass these, by default, the values are read from the `config/services.php` file. |
| 54 | + |
| 55 | +#### View |
| 56 | +On the view side, you have to include the Blade directive `@livewireRecaptcha`. This adds two scripts to the page, |
| 57 | +one for the reCAPTCHA script and one for the custom Livewire directive to hook into the form submission. |
| 58 | + |
| 59 | +Preferrably these scripts are only added to the page that has the Captcha-protected form (alternatively, you can add |
| 60 | +the `@livewireRecaptcha` directive on a higher level, lets say your layout). |
| 61 | + |
| 62 | +Secondly, add the new directive `wire:recaptcha` to the form element that you want to protect. |
| 63 | +```html |
| 64 | +<!-- some-livewire-component.blade.php --> |
| 65 | + |
| 66 | +<!-- (optional) Add error handling --> |
| 67 | +@if($errors->has('gRecaptchaResponse')) |
| 68 | + <div class="alert alert-danger">{{ $errors->first('gRecaptchaResponse') }}</div> |
| 69 | +@endif |
| 70 | + |
| 71 | +<!-- Add the `wire:recaptcha` Livewire directive --> |
| 72 | +<form wire:submit="save" wire:recaptcha> |
| 73 | + <!-- The rest of your form --> |
| 74 | + <button type="submit">Submit</button> |
| 75 | +</form> |
| 76 | + |
| 77 | +<!-- Add the `@livewireRecaptcha` Blade directive --> |
| 78 | +@livewireRecaptcha |
| 79 | +``` |
| 80 | + |
| 81 | +Also here, you are able to set the site-key manually for the directive using: |
| 82 | +```html |
| 83 | +@livewireRecaptcha('mysitekey') |
50 | 84 | ```
|
51 | 85 |
|
52 |
| -The google reCAPTCHA check will automatically occur before the actual form is submitted. Once |
53 |
| -the `save()` method is executed, the reCAPTCHA response has been successful. |
| 86 | +#### Finishing up |
| 87 | +The Google reCAPTCHA protection will automatically occur before the actual form is submitted. Before the `save()` method |
| 88 | +is executed, a serverside request will be sent to Google to verify the invisible Captcha challenge. Once the reCAPTCHA response |
| 89 | +has been successful, the actual `save()` method will be executed. |
54 | 90 |
|
55 |
| -When an error occurs with validation, a ValidationException is thrown for the key `gRecaptchaResponse`. |
| 91 | +#### Error handling |
| 92 | +When an error occurs with the Captcha validation, a ValidationException is thrown for the key `gRecaptchaResponse`. |
56 | 93 | There is a translatable error message for `'livewire-recaptcha::recaptcha.invalid_response'`.
|
0 commit comments