Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Throttle message

I'm using ThrottleRequest to throttle login attempts. In Kendler.php i have

'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,

and my route in web.php

Route::post('login', ['middleware' => 'throttle:3,1', 'uses' => 'Auth\LoginController@authenticate']);

When i login fourth time, it return status 429 with message 'TOO MANY REQUESTS.' (by default i guess)
But i just want to return error message, somethings like:

return redirect('/login')
            ->withErrors(['errors' => 'xxxxxxx']);

Anybody help me! THANK YOU!

like image 519
hayumi kuran Avatar asked Oct 11 '18 08:10

hayumi kuran


People also ask

What is throttle in laravel?

Laravel utilizes throttle middleware to limit the amount of traffic for a given route or gather of routes. The throttle middleware accepts two parameters that decide the maximum number of requests that can be made in a given number of minutes.

What is Ratelimiter in laravel?

Laravel includes a simple to use rate limiting abstraction which, in conjunction with your application's cache, provides an easy way to limit any action during a specified window of time. If you are interested in rate limiting incoming HTTP requests, please consult the rate limiter middleware documentation.

What is default rate limiter value in laravel?

Laravel has a fixed decay rate limiter. With default settings of throttle:60,1 this means that a client could make 60 requests with 1 minute of decay before hitting a 1 minute forced decay timeout. The client could make 60 requests in 1 second or distributed over 60 seconds at a rate of 1 request per second ( 1 r/s ).

Where do I register middleware in laravel?

Registering Middleware If you want a middleware to run during every HTTP request to your application, list the middleware class in the $middleware property of your app/Http/Kernel.php class.


1 Answers

You can either extend the middleware and override the buildException() method to change the message it passes when it throws a ThrottleRequestsException or you can use your exception handler to catch the ThrottleRequestsException and do whatever you want.

so in Exceptions/Handler.php you could do something like

use Illuminate\Http\Exceptions\ThrottleRequestsException;

public function render($request, Exception $exception)
{
    if ($exception instanceof ThrottleRequestsException) {
      //Do whatever you want here.
    }

    return parent::render($request, $exception);
}
like image 187
Joe Avatar answered Sep 27 '22 20:09

Joe