Skip to content

Commit b86c2b1

Browse files
committed
readme update
1 parent 576a886 commit b86c2b1

File tree

2 files changed

+71
-34
lines changed

2 files changed

+71
-34
lines changed

README.md

Lines changed: 70 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,93 @@
1-
# Installation
1+
# Livewire ReCAPTCHA v3
2+
[![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+
[![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+
[![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+
[![Total Downloads](https://img.shields.io/packagist/dt/dutchcodingcompany/livewire-recaptcha.svg?style=flat-square)](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
210
```shell
311
composer require dutchcodingcompany/livewire-recaptcha
412
```
513

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

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:
1918

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+
],
2227
```
28+
#### Component
29+
In your Livewire component, at your form submission method, add the `#[ValidatesRecaptcha]` attribute:
2330

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:
2831
```php
29-
// SomeComponent.php
32+
use Livewire\Component;
33+
34+
class SomeComponent extends Component
35+
{
3036
use DutchCodingCompany\LivewireRecaptcha\ValidatesRecaptcha;
3137

3238
// (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.
3440
public string $gRecaptchaResponse;
3541

36-
/*
37-
* (default) take keys from:
38-
* config('services.google.recaptcha.site_key')
39-
* config('services.google.recaptcha.secret_key')
40-
*/
4142
#[ValidatesRecaptcha]
42-
/*
43-
* (alternative) manually provide keys:
44-
*/
45-
#[ValidatesRecaptcha(siteKey: 'foo', secretKey: 'bar')]
4643
public function save(): mixed
4744
{
4845
// Your logic here will only be called if the captcha passes...
4946
}
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')
5084
```
5185

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

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`.
5693
There is a translatable error message for `'livewire-recaptcha::recaptcha.invalid_response'`.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"Google ReCaptcha",
88
"Laravel"
99
],
10-
"homepage": "https://github.com/dutchcodingcompany/filament-socialite",
10+
"homepage": "https://github.com/dutchcodingcompany/livewire-recaptcha",
1111
"license": "MIT",
1212
"require": {
1313
"php": "^8.2",

0 commit comments

Comments
 (0)