dutchcodingcompany/livewire-recaptcha
Installation:
composer require dutchcodingcompany/livewire-recaptcha
Publish the config file (if needed):
php artisan vendor:publish --provider="DutchCodingCompany\LivewireRecaptcha\LivewireRecaptchaServiceProvider"
Configure Google reCAPTCHA:
Add your SITE_KEY and SECRET_KEY to .env:
RECAPTCHA_SITE_KEY=your_site_key
RECAPTCHA_SECRET_KEY=your_secret_key
First Use Case: Protect a Livewire action (e.g., form submission) by adding the directive to the method:
use DutchCodingCompany\LivewireRecaptcha\HasRecaptcha;
class MyComponent extends Component
{
use HasRecaptcha;
public function submitForm()
{
$this->validate([...]);
// Your logic here
}
public function recaptcha()
{
return $this->recaptcha('submitForm'); // Protects 'submitForm'
}
}
Add reCAPTCHA Script: Include the script in your Livewire component's view:
@livewireScripts
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
HasRecaptcha trait in your Livewire component.$this->recaptcha('actionName') in a recaptcha() method to protect specific actions.public function recaptcha()
{
$this->recaptcha('submitForm'); // Protects submitForm
$this->recaptcha('deleteItem'); // Protects deleteItem
}
public function recaptcha()
{
if ($this->isAdmin) {
return; // Skip reCAPTCHA for admins
}
$this->recaptcha('submitForm');
}
v3):
$this->recaptcha('submitForm', version: 'v2'); // Uses v2
$this->recaptcha('submitForm', version: 'v2-invisible'); // Uses v2-invisible
public function submitForm()
{
if (!$this->verifyRecaptcha()) {
session()->flash('error', 'reCAPTCHA verification failed.');
return;
}
// Proceed with validation and logic
}
<div class="g-recaptcha" data-sitekey="{{ config('services.recaptcha.site_key') }}"></div>
config/livewire-recaptcha.php:
'threshold' => 0.9, // Default is 0.5
verifyRecaptcha() method for custom logic:
public function verifyRecaptcha()
{
if (auth()->check()) {
return true; // Skip for logged-in users
}
return parent::verifyRecaptcha();
}
recaptcha helper in tests to mock responses:
$this->recaptcha->shouldReceive('verify')->andReturn(true);
'language' => 'nl', // Dutch
Missing Script:
api.js) will cause silent failures. Always add it to your layout or component view.Incorrect Keys:
SITE_KEY or SECRET_KEY will result in failed verifications. Double-check .env:
RECAPTCHA_SITE_KEY=your_site_key
RECAPTCHA_SECRET_KEY=your_secret_key
Caching Issues:
Version Confusion:
Livewire Updates:
Check Logs:
APP_DEBUG=true) to see reCAPTCHA verification errors in Laravel logs.Verify API Responses:
SECRET_KEY to ensure it’s working.Inspect Network Requests:
https://www.google.com/recaptcha/api/siteverify
Test in Sandbox:
6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI) to avoid hitting request limits during development.Custom Verification Logic:
verifyRecaptcha() method to add custom rules (e.g., IP-based exemptions).Event Listeners:
recaptcha.verified) by extending the package or using Laravel events.Frontend Customization:
data-* attributes:
<div class="g-recaptcha"
data-sitekey="{{ config('services.recaptcha.site_key') }}"
data-theme="dark"
data-size="compact">
</div>
Rate Limiting:
throttle middleware).Multi-Tenant Support:
$siteKey = Tenant::find($tenantId)->recaptcha_site_key;
How can I help you explore Laravel packages today?