1
- # Livewire ReCAPTCHA v3
1
+ # Livewire ReCAPTCHA v3/v2/v2-invisible
2
+
2
3
[ ![ Latest Version on Packagist] ( https://img.shields.io/packagist/v/dutchcodingcompany/livewire-recaptcha.svg?style=flat-square )] ( https://packagist.org/packages/dutchcodingcompany/livewire-recaptcha )
3
4
[ ![ GitHub Tests Action Status] ( https://img.shields.io/github/workflow/status/dutchcodingcompany/livewire-recaptcha/run-tests?label=tests )] ( https://github.com/dutchcodingcompany/livewire-recaptcha/actions?query=workflow%3Arun-tests+branch%3Amain )
4
5
[ ![ GitHub Code Style Action Status] ( https://img.shields.io/github/workflow/status/dutchcodingcompany/livewire-recaptcha/Check%20&%20fix%20styling?label=code%20style )] ( https://github.com/dutchcodingcompany/livewire-recaptcha/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain )
5
6
[ ![ Total Downloads] ( https://img.shields.io/packagist/dt/dutchcodingcompany/livewire-recaptcha.svg?style=flat-square )] ( https://packagist.org/packages/dutchcodingcompany/livewire-recaptcha )
6
7
7
- This package provides a custom Livewire directive to protect your Livewire functions with a _ Google reCAPTCHA v3_ check.
8
+ This package provides a custom Livewire directive to protect your Livewire functions with a _ Google reCAPTCHA (v2 + v2
9
+ invisible + v3)_ check.
8
10
9
11
## Installation
12
+
10
13
``` shell
11
14
composer require dutchcodingcompany/livewire-recaptcha
12
15
```
13
16
14
17
## Configuration
15
- Next, read https://developers.google.com/recaptcha/docs/v3 on how to create your own key pair.
16
18
17
- The site key and secret key should be defined in your ` config/services.php ` file:
19
+ Read https://developers.google.com/recaptcha/intro on how to create your own key pair for the specific ReCaptcha
20
+ version you are going to implement.
21
+
22
+ This package supports the following versions. Note that each version requires a different sitekey/secretkey pair:
23
+
24
+ | ** Version** | ** Docs** | ** Notes** |
25
+ | ----------------------| -------------------------------------------------------------------| -----------------------------|
26
+ | ** v3** (recommended) | [ V3 Docs] ( https://developers.google.com/recaptcha/docs/v3 ) | |
27
+ | ** v2** | [ V2 Docs] ( https://developers.google.com/recaptcha/docs/display ) | |
28
+ | ** v2 invisible** | [ V2 Docs] ( https://developers.google.com/recaptcha/docs/invisible ) | Use ` 'size' => 'invisible' ` |
29
+
30
+ Your options should reside in the ` config/services.php ` file:
18
31
19
32
``` php
20
- // ...
21
- 'google' => [
22
- 'recaptcha' => [
23
- 'site_key' => env('GOOGLE_RECAPTCHA_SITE_KEY'),
24
- 'secret_key' => env('GOOGLE_RECAPTCHA_SECRET_KEY'),
25
- ]
26
- ],
33
+ // V3 config:
34
+ 'google' => [
35
+ 'recaptcha' => [
36
+ 'site_key' => env('GOOGLE_RECAPTCHA_SITE_KEY'),
37
+ 'secret_key' => env('GOOGLE_RECAPTCHA_SECRET_KEY'),
38
+ 'version' => 'v3',
39
+ 'score' => 0.5, // An integer between 0 and 1, that indicates the minimum score to pass the Captcha challenge.
40
+ ],
41
+ ],
42
+
43
+ // V2 config:
44
+ 'google' => [
45
+ 'recaptcha' => [
46
+ 'site_key' => env('GOOGLE_RECAPTCHA_SITE_KEY'),
47
+ 'secret_key' => env('GOOGLE_RECAPTCHA_SECRET_KEY'),
48
+ 'version' => 'v2',
49
+ 'size' => 'normal', // 'normal', 'compact' or 'invisible'.
50
+ 'theme' => 'light', // 'light' or 'dark'.
51
+ ],
52
+ ],
27
53
```
54
+
28
55
#### Component
56
+
29
57
In your Livewire component, at your form submission method, add the ` #[ValidatesRecaptcha] ` attribute:
30
58
31
59
``` php
@@ -46,26 +74,29 @@ class SomeComponent extends Component
46
74
}
47
75
}
48
76
```
49
- For fine-grained control, you can pass manual site and secret keys using:
77
+
78
+ For fine-grained control, you can pass a custom secret key and minimum score (applies only to V3) using:
79
+
50
80
``` php
51
- #[ValidatesRecaptcha(siteKey : 'mysitekey ', secretKey: 'mysecretkey' )]
81
+ #[ValidatesRecaptcha(secretKey : 'mysecretkey ', score: 0.9 )]
52
82
```
53
- If you do not pass these, by default, the values are read from the ` config/services.php ` file.
54
83
55
84
#### View
85
+
56
86
On the view side, you have to include the Blade directive ` @livewireRecaptcha ` . This adds two scripts to the page,
57
87
one for the reCAPTCHA script and one for the custom Livewire directive to hook into the form submission.
58
88
59
89
Preferrably these scripts are only added to the page that has the Captcha-protected form (alternatively, you can add
60
90
the ` @livewireRecaptcha ` directive on a higher level, lets say your layout).
61
91
62
92
Secondly, add the new directive ` wire:recaptcha ` to the form element that you want to protect.
93
+
63
94
``` html
64
95
<!-- some-livewire-component.blade.php -->
65
96
66
97
<!-- (optional) Add error handling -->
67
98
@if($errors->has('gRecaptchaResponse'))
68
- <div class =" alert alert-danger" >{{ $errors->first('gRecaptchaResponse') }}</div >
99
+ <div class =" alert alert-danger" >{{ $errors->first('gRecaptchaResponse') }}</div >
69
100
@endif
70
101
71
102
<!-- Add the `wire:recaptcha` Livewire directive -->
@@ -78,16 +109,24 @@ Secondly, add the new directive `wire:recaptcha` to the form element that you wa
78
109
@livewireRecaptcha
79
110
```
80
111
81
- Also here, you are able to set the site-key manually for the directive using:
112
+ You can override any of the configuration values using:
113
+
82
114
``` html
83
- @livewireRecaptcha('mysitekey')
115
+ @livewireRecaptcha(
116
+ version: 'v2',
117
+ siteKey: 'abcd_efgh-hijk_LMNOP',
118
+ theme: 'dark',
119
+ size: 'compact',
120
+ )
84
121
```
85
122
86
123
#### 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.
124
+
125
+ The Google ReCAPTCHA validation will automatically occur before the actual form is submitted. Before the ` save() ` method
126
+ is executed, a serverside request will be sent to Google to verify the Captcha challenge. Once the reCAPTCHA
127
+ response has been successful, your actual Livewire component method will be executed.
90
128
91
129
#### Error handling
130
+
92
131
When an error occurs with the Captcha validation, a ValidationException is thrown for the key ` gRecaptchaResponse ` .
93
- There is a translatable error message for ` 'livewire-recaptcha::recaptcha.invalid_response' ` .
132
+ There is a translatable error message available under ` 'livewire-recaptcha::recaptcha.invalid_response' ` .
0 commit comments