As a Laravel developer, I know how important it is to keep login systems secure, especially in apps dealing with sensitive data like health records, credit info, or hosting panels. That’s why I always recommend using Throttle Middleware in Laravel 12.
It helps protect your login routes from brute force attacks and keeps your application safe. In this guide, I’ll show you step-by-step how to implement throttle protection, with real examples, to help you build secure login systems with confidence.
Laravel's throttle middleware helps protect login routes by limiting the number of login attempts. This is crucial in sectors like insurance, law, and healthcare, where data protection is critical.
Open your routes/web.php and apply the throttle middleware to your login route.
Route::post('/login', [LoginController::class, 'authenticate'])
->middleware('throttle:5,1'); // 5 attempts per minute
This means users can only try to log in 5 times per minute. After that, they’ll be temporarily blocked.
You might want to return a custom error message or redirect. To do this, you can override the sendLockoutResponse method in your login controller.
use Illuminate\Cache\RateLimiter;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Illuminate\Support\Str;
protected function sendLockoutResponse(Request $request)
{
$seconds = app(RateLimiter::class)->availableIn(
$this->throttleKey($request)
);
throw ValidationException::withMessages([
'email' => [__('Too many attempts. Try again in :seconds seconds.', ['seconds' => $seconds])],
])->status(429);
}
If you're building APIs (say, for loan applications or donation portals), you can secure those too:
Route::middleware('throttle:10,1')->post('/api/login', [ApiAuthController::class, 'login']);
Try logging in multiple times to ensure throttling works. You should get blocked after exceeding the limit. This is essential for apps handling credit, mortgage, or hosting data.
Implementing Laravel 12’s throttle middleware is a quick but powerful way to protect your login forms from brute force attacks. Especially if you're building applications in high-risk niches like insurance, health, or finance, throttle protection is a must. By following the steps above, you can improve your app’s security, trust, and reliability.
Frequently Asked Questions (FAQs)
It limits the number of requests (like login attempts) from a user or IP within a set timeframe, helping to prevent brute-force attacks.
No, you can apply it to any route — including registration, password reset, or APIs — that needs protection against repeated access.
Yes. Use throttle:X,Y
where X
is the number of allowed attempts and Y
is the number of minutes.
It helps protect sensitive systems by blocking automated attacks, making it essential for platforms that handle credit, health, legal, or insurance data.
Yes. Laravel returns a 429 Too Many Requests
response, and you can customize this message for a better user experience.
You might also like: