32 lines
725 B
PHP
32 lines
725 B
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use Closure;
|
|
use Illuminate\Cache\RateLimiter;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class RateLimitPrompts
|
|
{
|
|
protected $limiter;
|
|
|
|
public function __construct(RateLimiter $limiter)
|
|
{
|
|
$this->limiter = $limiter;
|
|
}
|
|
|
|
public function handle($request, Closure $next)
|
|
{
|
|
$key = 'prompts:' . $request->user()->id;
|
|
|
|
if ($this->limiter->tooManyAttempts($key, 60)) { // 60 attempts per minute
|
|
return response()->json([
|
|
'error' => 'Too many requests. Please try again later.'
|
|
], Response::HTTP_TOO_MANY_REQUESTS);
|
|
}
|
|
|
|
$this->limiter->hit($key);
|
|
|
|
return $next($request);
|
|
}
|
|
}
|