- How do I add rate limiting to a Livewire action like a form submission?
- Use the `@rateLimited` directive on your Livewire method or wrap it with the `withRateLimiting()` helper. For example, add `@rateLimited('submit-form', maxAttempts: 5, decaySeconds: 10)` above your `submitForm()` method. This integrates with Laravel’s RateLimiter for consistent throttling.
- Does this package work with Laravel 10 and Livewire 3.x?
- Yes, **danharrin/livewire-rate-limiting** is designed for Laravel 10+ and Livewire 3.x. If you’re using older versions, you may need to upgrade to leverage its full feature set, including Livewire 3’s action system optimizations.
- Can I customize the rate limit keys (e.g., per user, per IP, or per session)?
- Absolutely. The package supports custom keys via Laravel’s RateLimiter. Use `Cache::tags()` or define a custom resolver in your `RateLimiter` configuration to apply limits per user, IP, or session dynamically.
- What happens when a user exceeds the rate limit? Does it break the Livewire component?
- By default, the package throws a `TooManyRequestsException`, which you can catch in your Livewire component to show a user-friendly message (e.g., 'Too many attempts. Please wait.'). You can also customize this behavior via exceptions or return a JSON response.
- Will this package slow down my Livewire components in production?
- The performance impact is minimal since it leverages Laravel’s caching layer (Redis recommended). However, high-concurrency actions may require a dedicated cache instance or sharding to avoid bottlenecks. Test under load to ensure responsiveness.
- Can I use this for login attempts or password reset requests?
- Yes, it’s ideal for securing sensitive actions like login attempts or password resets. Configure strict limits (e.g., 5 attempts per 5 minutes) and pair it with Laravel’s `ThrottlesLogins` trait for added security.
- How do I test rate-limited Livewire actions in PHPUnit?
- Mock Laravel’s `RateLimiter` facade in your tests to simulate exceeded limits. Use `RateLimiter::for('key')->hit()` to trigger throttling, then verify your component’s response or exception handling. For Livewire-specific tests, use `Livewire::test()` to simulate user interactions.
- Is there a way to log rate-limit events for abuse detection?
- Yes, extend the package by listening to Laravel’s `illuminate.events.throttled` event or log manually via `Log::channel('rate-limits')->info()`. This helps track suspicious activity or monitor usage patterns.
- Can I apply rate limiting to all actions in a Livewire component at once?
- Not directly, but you can create a base component with a `boot()` method that applies rate limiting to all actions dynamically. Use middleware-like logic or a trait to wrap methods, though this requires careful handling of Livewire’s lifecycle.
- What are the alternatives to this package for Livewire rate limiting?
- Alternatives include Laravel’s built-in `throttle` middleware (for HTTP routes) or custom Livewire middleware. However, this package is specialized for Livewire actions, offering tighter integration, per-action granularity, and seamless exception handling without HTTP overhead.