Skip to content

Commit 257a139

Browse files
wip
1 parent 9a610eb commit 257a139

File tree

3 files changed

+196
-19
lines changed

3 files changed

+196
-19
lines changed

README.md

Lines changed: 183 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# This is my package laravel-google-recaptcha-v3
1+
# Laravel Google reCAPTCHA v3
22

33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/maize-tech/laravel-google-recaptcha-v3.svg?style=flat-square)](https://packagist.org/packages/maize-tech/laravel-google-recaptcha-v3)
44
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/maize-tech/laravel-google-recaptcha-v3/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/maize-tech/laravel-google-recaptcha-v3/actions?query=workflow%3Arun-tests+branch%3Amain)
55
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/maize-tech/laravel-google-recaptcha-v3/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/maize-tech/laravel-google-recaptcha-v3/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
66
[![Total Downloads](https://img.shields.io/packagist/dt/maize-tech/laravel-google-recaptcha-v3.svg?style=flat-square)](https://packagist.org/packages/maize-tech/laravel-google-recaptcha-v3)
77

8-
This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
8+
A Laravel package that provides a simple and elegant integration of Google reCAPTCHA v3 for your Laravel applications. This package includes a Blade directive for easy frontend integration and a validation rule for backend verification with customizable score thresholds.
99

1010
## Support us
1111

@@ -23,39 +23,207 @@ You can install the package via composer:
2323
composer require maize-tech/laravel-google-recaptcha-v3
2424
```
2525

26-
You can publish and run the migrations with:
26+
You can install and configure the package with:
2727

2828
```bash
29-
php artisan vendor:publish --tag="laravel-google-recaptcha-v3-migrations"
30-
php artisan migrate
29+
php artisan laravel-google-recaptcha-v3:install
3130
```
3231

33-
You can publish the config file with:
34-
35-
```bash
36-
php artisan vendor:publish --tag="laravel-google-recaptcha-v3-config"
37-
```
32+
This command will publish the configuration file.
3833

3934
This is the contents of the published config file:
4035

4136
```php
4237
return [
38+
/*
39+
|--------------------------------------------------------------------------
40+
| Enabled
41+
|--------------------------------------------------------------------------
42+
|
43+
| This option controls whether Google reCAPTCHA v3 is enabled.
44+
| When disabled, the validation will be skipped.
45+
|
46+
*/
47+
'enabled' => env('GOOGLE_RECAPTCHA_V3_ENABLED', true),
48+
49+
/*
50+
|--------------------------------------------------------------------------
51+
| Site Key
52+
|--------------------------------------------------------------------------
53+
|
54+
| Your Google reCAPTCHA v3 site key.
55+
| You can get this from https://www.google.com/recaptcha/admin
56+
|
57+
*/
58+
'site_key' => env('GOOGLE_RECAPTCHA_V3_SITE_KEY'),
59+
60+
/*
61+
|--------------------------------------------------------------------------
62+
| Secret Key
63+
|--------------------------------------------------------------------------
64+
|
65+
| Your Google reCAPTCHA v3 secret key.
66+
| You can get this from https://www.google.com/recaptcha/admin
67+
|
68+
*/
69+
'secret_key' => env('GOOGLE_RECAPTCHA_V3_SECRET_KEY'),
70+
71+
/*
72+
|--------------------------------------------------------------------------
73+
| Score Threshold
74+
|--------------------------------------------------------------------------
75+
|
76+
| The minimum score threshold for the reCAPTCHA validation.
77+
| Google reCAPTCHA v3 returns a score (1.0 is very likely a good interaction,
78+
| 0.0 is very likely a bot). Default is 0.5.
79+
|
80+
*/
81+
'score_threshold' => env('GOOGLE_RECAPTCHA_V3_SCORE_THRESHOLD', 0.5),
4382
];
4483
```
4584

46-
Optionally, you can publish the views using
85+
## Configuration
4786

48-
```bash
49-
php artisan vendor:publish --tag="laravel-google-recaptcha-v3-views"
87+
After installing the package, add the following environment variables to your `.env` file:
88+
89+
```env
90+
GOOGLE_RECAPTCHA_V3_ENABLED=true
91+
GOOGLE_RECAPTCHA_V3_SITE_KEY=your-site-key-here
92+
GOOGLE_RECAPTCHA_V3_SECRET_KEY=your-secret-key-here
93+
GOOGLE_RECAPTCHA_V3_SCORE_THRESHOLD=0.5
5094
```
5195

96+
You can obtain your site key and secret key from the [Google reCAPTCHA Admin Console](https://www.google.com/recaptcha/admin).
97+
5298
## Usage
5399

100+
### Frontend Integration
101+
102+
Add the reCAPTCHA script to your Blade templates using the `@recaptcha` directive. You can customize the badge position by passing one of the available badge positions:
103+
104+
```blade
105+
<!DOCTYPE html>
106+
<html>
107+
<head>
108+
<title>My Form</title>
109+
</head>
110+
<body>
111+
<form id="myForm" method="POST" action="/submit">
112+
@csrf
113+
<!-- Your form fields -->
114+
<input type="hidden" name="g-recaptcha-response" id="g-recaptcha-response">
115+
<button type="submit">Submit</button>
116+
</form>
117+
118+
@recaptcha(\Maize\GoogleRecaptchaV3\Enums\Badge::BOTTOMRIGHT)
119+
120+
<script>
121+
document.getElementById('myForm').addEventListener('submit', async function(e) {
122+
e.preventDefault();
123+
124+
const token = await window.recaptcha('submit');
125+
document.getElementById('g-recaptcha-response').value = token;
126+
127+
this.submit();
128+
});
129+
</script>
130+
</body>
131+
</html>
132+
```
133+
134+
#### Available Badge Positions
135+
136+
- `Badge::INLINE` - Displays the badge inline
137+
- `Badge::BOTTOMLEFT` - Displays the badge at the bottom left
138+
- `Badge::BOTTOMRIGHT` - Displays the badge at the bottom right (recommended)
139+
140+
### Backend Validation
141+
142+
Use the `googleRecaptchaV3` validation rule in your form requests or controllers:
143+
54144
```php
55-
$googleRecaptchaV3 = new Maize\GoogleRecaptchaV3();
56-
echo $googleRecaptchaV3->echoPhrase('Hello, Maize!');
145+
use Illuminate\Support\Facades\Validator;
146+
147+
$validator = Validator::make($request->all(), [
148+
'name' => 'required|string',
149+
'email' => 'required|email',
150+
'g-recaptcha-response' => ['required', 'string', Rule::googleRecaptchaV3()],
151+
]);
57152
```
58153

154+
#### Custom Score Threshold
155+
156+
You can override the default score threshold (configured in `config/google-recaptcha-v3.php`) by passing a custom threshold to the validation rule:
157+
158+
```php
159+
use Illuminate\Support\Facades\Validator;
160+
use Illuminate\Validation\Rule;
161+
162+
$validator = Validator::make($request->all(), [
163+
'g-recaptcha-response' => ['required', 'string', Rule::googleRecaptchaV3(0.7)],
164+
]);
165+
```
166+
167+
A higher threshold (e.g., 0.7 or 0.8) means stricter validation, while a lower threshold (e.g., 0.3 or 0.4) is more permissive.
168+
169+
### Form Request Example
170+
171+
```php
172+
<?php
173+
174+
namespace App\Http\Requests;
175+
176+
use Illuminate\Foundation\Http\FormRequest;
177+
use Illuminate\Validation\Rule;
178+
179+
class ContactFormRequest extends FormRequest
180+
{
181+
public function authorize(): bool
182+
{
183+
return true;
184+
}
185+
186+
public function rules(): array
187+
{
188+
return [
189+
'name' => ['required', 'string', 'max:255'],
190+
'email' => ['required', 'email', 'max:255'],
191+
'message' => ['required', 'string', 'max:1000'],
192+
'g-recaptcha-response' => ['required', 'string', Rule::googleRecaptchaV3()],
193+
];
194+
}
195+
196+
public function messages(): array
197+
{
198+
return [
199+
'g-recaptcha-response.required' => 'Please complete the reCAPTCHA verification.',
200+
];
201+
}
202+
}
203+
```
204+
205+
### JavaScript Helper
206+
207+
The package automatically provides a global `window.recaptcha()` function that you can use to get the reCAPTCHA token:
208+
209+
```javascript
210+
// Get token with default action 'submit'
211+
const token = await window.recaptcha();
212+
213+
// Get token with custom action
214+
const token = await window.recaptcha('login');
215+
```
216+
217+
This function returns a Promise that resolves to the reCAPTCHA token, or `null` if reCAPTCHA is not available or fails.
218+
219+
### Disabling reCAPTCHA
220+
221+
You can disable reCAPTCHA validation by setting the `enabled` configuration to `false` or by setting the `GOOGLE_RECAPTCHA_V3_ENABLED` environment variable to `false`. This is useful for local development or testing environments.
222+
223+
When disabled:
224+
- The `@recaptcha` directive will not render any scripts
225+
- The validation rule will pass without making any API calls to Google
226+
59227
## Testing
60228

61229
```bash

composer.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
{
22
"name": "maize-tech/laravel-google-recaptcha-v3",
3-
"description": "This is my package laravel-google-recaptcha-v3",
3+
"description": "Laravel package for Google reCAPTCHA v3 integration with form validation and spam protection",
44
"keywords": [
5-
"Maize",
65
"laravel",
7-
"laravel-google-recaptcha-v3"
6+
"google-recaptcha",
7+
"recaptcha-v3",
8+
"spam-protection",
9+
"form-validation",
10+
"security",
11+
"maize"
812
],
913
"homepage": "https://github.com/maize-tech/laravel-google-recaptcha-v3",
1014
"license": "MIT",

src/GoogleRecaptchaV3ServiceProvider.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ public function configurePackage(Package $package): void
1212
{
1313
$package
1414
->name('laravel-google-recaptcha-v3')
15-
->hasConfigFile();
15+
->hasConfigFile()
16+
->hasInstallCommand(fn (InstallCommand $command) => (
17+
$command
18+
->publishConfigFile()
19+
->askToStarRepoOnGitHub('maize-tech/laravel-google-recaptcha-v3')
20+
));
1621
}
1722

1823
public function packageBooted(): void

0 commit comments

Comments
 (0)