How to Set Limit Login Attempts in Laravel?

By Hardik Savani November 5, 2023 Category : Laravel

Hello Guys,

Today, I will let you know example of how to set limit login attempts in laravel. It's a simple example of block user after 3 attempts in laravel. let’s discuss about laravel too many attempts login. This article will give you a simple example of laravel throttle lockout time increase.

Here, i will give you following two ways to increase throttle limit in laravel login. so, let's see both simple solution.

Solution 1:

you need to overwrite default values by adding $maxAttempts and $decayMinutes. so you can see the LoginController.php code:

app/Http/Controllers/Auth/LoginController.php

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;

use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller

{

protected $maxAttempts = 3; /* Default is 5 */

protected $decayMinutes = 2; /* Default is 1 */

/*

|--------------------------------------------------------------------------

| Login Controller

|--------------------------------------------------------------------------

|

| This controller handles authenticating users for the application and

| redirecting them to your home screen. The controller uses a trait

| to conveniently provide its functionality to your applications.

|

*/

use AuthenticatesUsers;

/**

* Where to redirect users after login.

*

* @var string

*/

protected $redirectTo = '/home';

/**

* Create a new controller instance.

*

* @return void

*/

public function __construct()

{

$this->middleware('guest', ['except' => 'logout']);

}

}

Solution 2:

we can do it on hasTooManyLoginAttempts() method update. so you can see the LoginController.php code:

app/Http/Controllers/Auth/LoginController.php

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;

use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller

{

/*

|--------------------------------------------------------------------------

| Login Controller

|--------------------------------------------------------------------------

|

| This controller handles authenticating users for the application and

| redirecting them to your home screen. The controller uses a trait

| to conveniently provide its functionality to your applications.

|

*/

use AuthenticatesUsers;

/**

* Where to redirect users after login.

*

* @var string

*/

protected $redirectTo = '/home';

/**

* Create a new controller instance.

*

* @return void

*/

public function __construct()

{

$this->middleware('guest', ['except' => 'logout']);

}

/**

* Determine if the user has too many failed login attempts.

*

* @param \Illuminate\Http\Request $request

* @return bool

*/

protected function hasTooManyLoginAttempts(Request $request)

{

return $this->limiter()->tooManyAttempts(

$this->throttleKey($request), 6, 30

);

}

}

I hope it can help you...

Tags :
Shares