Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ version you are going to implement.

This package supports the following versions. Note that each version requires a different sitekey/secretkey pair:

| **Version** | **Docs** | **Notes** |
|----------------------|-------------------------------------------------------------------|-----------------------------|
| **v3** (recommended) | [V3 Docs](https://developers.google.com/recaptcha/docs/v3) | |
| **v2** | [V2 Docs](https://developers.google.com/recaptcha/docs/display) | |
| **v2 invisible** | [V2 Docs](https://developers.google.com/recaptcha/docs/invisible) | Use `'size' => 'invisible'` |
| **Version** | **Docs** | **Notes** |
|----------------------|-------------------------------------------------------------------|-----------------------------------|
| **v3** (recommended) | [V3 Docs](https://developers.google.com/recaptcha/docs/v3) | |
| **v3** (enterprise) | [V3 Docs](https://developers.google.com/recaptcha/docs/v3) | Use `'version' => 'v3-enterprise'` |
| **v2** | [V2 Docs](https://developers.google.com/recaptcha/docs/display) | |
| **v2 invisible** | [V2 Docs](https://developers.google.com/recaptcha/docs/invisible) | Use `'size' => 'invisible'` |

Your options should reside in the `config/services.php` file:

Expand All @@ -38,6 +39,7 @@ Your options should reside in the `config/services.php` file:
'secret_key' => env('GOOGLE_RECAPTCHA_SECRET_KEY'),
'version' => 'v3',
'score' => 0.5, // An integer between 0 and 1, that indicates the minimum score to pass the Captcha challenge.
'endpoint' => 'https://www.google.com/recaptcha/api/siteverify', // For enterprise users, fill in your URL from google console (https://recaptchaenterprise.googleapis.com/v1/projects/project-name/assessments?key=API_KEY
],
],

Expand Down Expand Up @@ -113,10 +115,10 @@ You can override any of the configuration values using:

```html
@livewireRecaptcha(
version: 'v2',
siteKey: 'abcd_efgh-hijk_LMNOP',
theme: 'dark',
size: 'compact',
version: 'v2',
siteKey: 'abcd_efgh-hijk_LMNOP',
theme: 'dark',
size: 'compact',
)
```

Expand Down
4 changes: 3 additions & 1 deletion src/ValidatesRecaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ class ValidatesRecaptcha extends LivewireAttribute
public function __construct(
public ?string $secretKey = null,
public ?float $score = null,
public ?string $endpoint = null,
) {
$this->secretKey ??= config('services.google.recaptcha.secret_key');
$this->score ??= config('services.google.recaptcha.score') ?? 0.5;
$this->endpoint ??= config('services.google.recaptcha.endpoint', 'https://www.google.com/recaptcha/api/siteverify');
}

/**
Expand All @@ -29,7 +31,7 @@ public function __construct(
public function call(array $params, Closure $returnEarly): void
{
if (isset($this->component->gRecaptchaResponse)) {
$response = Http::asForm()->post('https://www.google.com/recaptcha/api/siteverify', [
$response = Http::asForm()->post($this->endpoint, [
'secret' => $this->secretKey,
'response' => $this->component->gRecaptchaResponse,
'remoteip' => request()->ip(),
Expand Down
29 changes: 29 additions & 0 deletions src/directive.recaptcha.v3-enterprise.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<script>
document.addEventListener('livewire:init', () => {
Livewire.directive('recaptcha', ({ el, directive, component, cleanup }) => {
const submitExpression = (() => {
for (const attr of el.attributes) {
if (attr.name.startsWith('wire:submit')) {
return attr.value;
}
}
})();

const onSubmit = (e) => {
e.preventDefault();
e.stopImmediatePropagation();

grecaptcha.enterprise.ready(async () => {
const token = await grecaptcha.enterprise.execute(@json($siteKey), { action: 'submit' });

component.$wire.$set('gRecaptchaResponse', token).then(() => {
Alpine.evaluate(el, "$wire." + submitExpression, { scope: { $event: e } });
});
});
}

el.addEventListener('submit', onSubmit, { capture: true });
});
});
</script>
<script src="https://www.google.com/recaptcha/enterprise.js?render={{ $siteKey }}"></script>