shimana's avatar

Too Many Login Attempts

I am using Laravel 11 and Jetstream. For the login, after 5 unsuccessful attempts, an error page with code 429 is displayed to me, as shown in the image below.

MarineGEO circle logo

However, I need the error message, along with the remaining time, to be displayed on the same login page. As shown in the image below.

MarineGEO circle logo

in FortifyServiceProvider

    public function boot(): void
    {
        RateLimiter::for('login', function (Request $request) {
            $throttleKey = Str::transliterate(Str::lower($request->input(Fortify::username())).'|'.$request->ip());
            return Limit::perMinute(5)->by($throttleKey);
        });
....

I would appreciate it if you could guide me on how to display the error message as shown in the image above.

0 likes
3 replies
tykus's avatar
tykus
Best Answer
Level 104

You can set a Response callback where you can get the remaining time as well

public function boot(): void
{
  RateLimiter::for('login', function (Request $request) {
    $throttleKey = Str::transliterate(Str::lower($request->input(Fortify::username())).'|'.$request->ip());
    return Limit::perMinute(5)->by($throttleKey)->response(function ($request, $params) {
      return back()->withErrors([
        'message' => "Too many login attempts. Please try again in {$params['Retry-After']} seconds"
      ]);
    });
  });
}
1 like
shimana's avatar

@tykus Thank you for your help; your guidance resolved my issue.

tykus's avatar

@shimana note that you also have the option to write an implementation of Laravel\Fortify\Contracts\LockoutResponse and bind it into the container if the logic requires it

1 like

Please or to participate in this conversation.