How to Set Limit Login Attempts in 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel JQuery Ajax Loading Spinner Example
- Laravel Carbon Time Format AM PM Example Code
- How to use Carbon in Laravel Blade or Controller File?
- How to Store Array in Database Laravel?
- FCM Push Notification in Laravel Example
- Laravel Install Tailwind CSS Example
- Laravel 9 Create Multi Language Website Tutorial
- How to Run Laravel Project on Different Port?
- Laravel Collection Get Unique Values Example
- How to use Google Recaptcha V3 in Laravel App?
- Laravel Blade Isset Else Example
- Laravel Carbon Check If Date is Greater Than Other Date
- Laravel Gates and Policies Tutorial with Example