### Install Livewire Rate Limiting Package Source: https://github.com/danharrin/livewire-rate-limiting/blob/main/README.md Use Composer to add the danharrin/livewire-rate-limiting package to your Laravel project's dependencies. This command downloads and installs the package. ```bash composer require danharrin/livewire-rate-limiting ``` -------------------------------- ### Livewire Component Rate Limiting Methods API Source: https://github.com/danharrin/livewire-rate-limiting/blob/main/README.md Reference for the methods provided by the `WithRateLimiting` trait. Includes `rateLimit` for applying limits, `hitRateLimiter` for incrementing the counter without consequence, and `clearRateLimiter` for resetting the limit. ```php use DanHarrin\LivewireRateLimiting\WithRateLimiting; /** * Rate limit a Livewire method, `$maxAttempts` times every `$decaySeconds` seconds. * * @throws DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException */ $this->rateLimit( $maxAttempts, // The number of times that the rate limit can be hit in the given decay period. $decaySeconds = 60, // The length of the decay period in seconds. By default, this is a minute. $method, // The name of the method that is being rate limited. By default, this is set to the method that `$this->rateLimit()` is called from. ); /** * Hit a method's rate limiter without consequence. */ $this->hitRateLimiter( $method, // The name of the method that is being rate limited. By default, this is set to the method that `$this->hitRateLimiter()` is called from. $decaySeconds = 60, // The length of the decay period in seconds. By default, this is a minute. ); /** * Clear a method's rate limiter. */ $this->clearRateLimiter( $method, // The name of the method that is being rate limited. By default, this is set to the method that `$this->clearRateLimiter()` is called from. ); ``` -------------------------------- ### Implement Rate Limiting on Livewire Action Source: https://github.com/danharrin/livewire-rate-limiting/blob/main/README.md Apply rate limiting to a specific Livewire method using `$this->rateLimit()`. Wrap the call in a try-catch block to handle the `TooManyRequestsException` and provide user feedback, such as a validation error indicating the wait time. ```php rateLimit(10); } catch (TooManyRequestsException $exception) { throw ValidationException::withMessages([ 'email' => "Slow down! Please wait another {$exception->secondsUntilAvailable} seconds to log in.", ]); } // ... } } ``` -------------------------------- ### Apply Rate Limiting Trait in Livewire Source: https://github.com/danharrin/livewire-rate-limiting/blob/main/README.md To enable rate limiting on a Livewire component, apply the `WithRateLimiting` trait provided by the package. This makes the rate limiting methods available within the component. ```php rateLimit(10); } catch (TooManyRequestsException $exception) { $exception->component; // Class of the component that the rate limit was hit within. $exception->ip; // IP of the user that has hit the rate limit. $exception->method; // Name of the method that has hit the rate limit. $exception->minutesUntilAvailable; // Number of minutes until the rate limit is lifted, rounded up. $exception->secondsUntilAvailable; // Number of seconds until the rate limit is lifted. } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.