You cannot send a full object through to a redirect. Laravel needs to convert it a session header for the redirect. You will need to send only some identifier which you use to reconstruct it in the captcha page
Rate Limiter: Serialization of 'Closure' is not allowed
Hey guys,
I'm trying to integrate the https://github.com/artisansdk/ratelimiter package into my laravel application.
The package is a rate limiter that allows us to punish anyone who violates the request rate.
By default it generates a status code 429 when the limit is exceeded and I want to change this behavior by redirecting the request to a route where a captcha must be solved.
For that I created a middleware that extends from the ArtisanSdk\RateLimiter\Middleware class and put the redirection.
<?php
namespace App\Http\Middleware;
use ArtisanSdk\RateLimiter\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
class Throttle extends Middleware
{
public function handle($request, Closure $next, ...$args)
{
$resolver = $this->makeResolver($request, $args);
$limiter = $this->configureLimiter($resolver);
if ($limiter->exceeded()) {
$limiter->timeout($resolver->duration());
return redirect()->route('captcha')->with('limiter', $limiter);
}
$limiter->hit();
$response = $next($request);
return $this->addHeaders($response,
$limiter->limit(),
$limiter->remaining()
);
}
}
Now I need to send the limiter to the route, so I can easily do $limiter->reset() when the user solves the captcha, but I'm not able to send the limiter.
When I try to do return redirect()->route('captcha')->with('limiter', $limiter); I get exception Serialization of 'Closure' is not allowed.
Could you help me to get the $limiter on the route? Or suggest me another approach?
Please or to participate in this conversation.