Pablo
Pablo

Reputation: 1435

:seconds to :hours in Laravel Throttle?

In my

resources/lang/en/auth.php

I have this

<?php

return [

/*
|--------------------------------------------------------------------------
| Authentication Language Lines
|--------------------------------------------------------------------------
|
| The following language lines are used during authentication for various
| message that we need to display to the user. You are free to modify
| these language lines according to your application's requirements.
|
*/

'failed' => 'These credentials do not match our records.',
'throttle' => 'Too many login attempts. Please try again in :seconds seconds.',

];

Is there a way to edit the throttle :seconds to an hour?

the error message on the front would be something like this

Too many login attempts. Please try again in 2 hour(s).

Upvotes: 1

Views: 795

Answers (1)

Muhammad Dyas Yaskur
Muhammad Dyas Yaskur

Reputation: 8088

By default, the throttle login trait only give minutes and seconds, if you want to add hours just override sendLockoutResponse method from Illuminate\Foundation\Auth\ThrottlesLogins like below(in LoginController that use AuthenticatesUsers trait ):

/**
 * Redirect the user after determining they are locked out.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return void
 *
 * @throws \Illuminate\Validation\ValidationException
 */
protected function sendLockoutResponse(Request $request)
{
    $seconds = $this->limiter()->availableIn(
        $this->throttleKey($request)
    );

    throw ValidationException::withMessages([
        $this->username() => [trans('auth.throttle', [
            'seconds' => $seconds,
            'minutes' => ceil($seconds / 60),
            'hours' => ceil($seconds / 60 / 60),
        ])],
    ])->status(Response::HTTP_TOO_MANY_REQUESTS);
}

Now you can add hours throttles like below:

'throttle' => 'Too many login attempts. Please try again in :hours hour(s).',

Upvotes: 3

Related Questions