|
| 1 | +# darvis/livewire-honeypot |
| 2 | + |
| 3 | +Lightweight **honeypot + time‑trap** protection for **Livewire 3** (Laravel 11). |
| 4 | +Blocks simple bots without CAPTCHAs, privacy‑friendly and unobtrusive. |
| 5 | + |
| 6 | +## Features |
| 7 | +- 🪤 Honeypot bait field (`present|size:0`) |
| 8 | +- ⏱️ Time‑trap (minimum fill time) |
| 9 | +- 🧩 Works as **Trait** for Livewire and as **Service** for controllers/APIs |
| 10 | +- 🧱 Blade component `<x-honeypot />` for easy inclusion |
| 11 | +- 🔌 Zero dependencies beyond Livewire 3 / Laravel 11 |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```bash |
| 16 | +composer require darvis/livewire-honeypot |
| 17 | +``` |
| 18 | + |
| 19 | +(For local development, you can add a `path` repository in your app's `composer.json`.) |
| 20 | + |
| 21 | +## Usage — Livewire (Trait) |
| 22 | + |
| 23 | +1) In your Livewire component: |
| 24 | + |
| 25 | +```php |
| 26 | +use Darvis\LivewireHoneypot\Traits\HasHoneypot; |
| 27 | + |
| 28 | +class ContactForm extends Component |
| 29 | +{ |
| 30 | + use HasHoneypot; |
| 31 | + |
| 32 | + public string $name = ''; |
| 33 | + public string $email = ''; |
| 34 | + public string $message = ''; |
| 35 | + |
| 36 | + public function submit(): void |
| 37 | + { |
| 38 | + $this->validate([ |
| 39 | + 'name' => 'required|string|min:2', |
| 40 | + 'email' => 'required|email', |
| 41 | + 'message' => 'required|string|min:10', |
| 42 | + ]); |
| 43 | + |
| 44 | + $this->validateHoneypot(); |
| 45 | + |
| 46 | + // process form ... |
| 47 | + |
| 48 | + $this->reset(['name','email','message']); |
| 49 | + $this->resetHoneypot(); |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +2) In your Blade (or Flux) view, add the component (place anywhere inside the form): |
| 55 | + |
| 56 | +```blade |
| 57 | +<x-honeypot /> |
| 58 | +``` |
| 59 | + |
| 60 | +## Usage — Controller / API (Service) |
| 61 | + |
| 62 | +```php |
| 63 | +use Darvis\LivewireHoneypot\Services\HoneypotService; |
| 64 | + |
| 65 | +public function store(Request $request, HoneypotService $honeypot) |
| 66 | +{ |
| 67 | + $honeypot->validate($request->only('hp_website', 'hp_started_at', 'hp_token')); |
| 68 | + // process form ... |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +To generate fields server‑side (non‑Livewire forms): |
| 73 | + |
| 74 | +```php |
| 75 | +$hp = app(Darvis\LivewireHoneypot\Services\HoneypotService::class)->generate(); |
| 76 | +// pass $hp to your view to prefill hidden inputs |
| 77 | +``` |
| 78 | + |
| 79 | +## Publishing the Blade view (optional) |
| 80 | +```bash |
| 81 | +php artisan vendor:publish --tag=livewire-honeypot-views |
| 82 | +``` |
| 83 | + |
| 84 | +## Throttling (recommended) |
| 85 | +Add request rate‑limiting on your form route: |
| 86 | + |
| 87 | +```php |
| 88 | +Route::get('/contact', \App\Livewire\ContactForm::class)->middleware('throttle:10,1'); |
| 89 | +``` |
| 90 | + |
| 91 | +## License |
| 92 | +MIT © Arvid de Jong (info@arvid.nl) |
0 commit comments