Koeno
Koeno

Reputation: 1573

Laravel RateLimiter/throttle increasing decay minutes

In Laravel you can throttle certain requests. For example you can throttle your login requests by max 5 attempts per minute via middleware or a RateLimiter:

    RateLimiter::for('login', function (Request $request) {
        return (new Limit('login', '5', 1))
            ->by($request->email.$request->ip());
    });

In this code example, the '5' means the max amount of requests per minute, and the 1 means the decay minutes (every time the limit has been reached, the user will be locked out for 1 minute).

However, how do you increase the decayMinutes everytime the limit has been reached? For example, how would you double this amount each time?

Upvotes: 4

Views: 3919

Answers (1)

Djave
Djave

Reputation: 9329

Info: I'm using Breeze with Laravel 8, which installs app\Requests\Auth\LoginRequest. This file is where I make the changes.

There were two important lines of code in order to customise the Laravel Breeze attempts and decay time. If you just want to hunt them out:

Attempts

RateLimiter::tooManyAttempts($this->throttleKey(), 5)

Decay time

RateLimiter::hit($this->throttleKey());

RateLimiter::tooManyAttempts (you can see) takes a number as its second argument (above 5) this is how many attempts someone is allowed.

RateLimiter::hit takes a second argument not shown, I was able to up mine to 5 minutes by adding it in:

RateLimiter::hit($this->throttleKey(), 300);

Upvotes: 1

Related Questions