Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit login attempts in Laravel 5.7

I have Laravel 5.7 project with custom login. How can I let Laravel accept three login attempts after that redirect for page waiting to 2 or 3 min, etc?

public function loginPost(LoginRequest $request)
{
    if (Auth::attempt(array('user_name' => $request->user_name, 'password' => $request->user_pass)))
    {
        if(Auth::check())
            return redirect('/');
        else
            return back();
    }
    else
    {
        return "login faled call administrator";
    }
}
like image 274
Awar Pulldozer Avatar asked Oct 05 '18 19:10

Awar Pulldozer


Video Answer


1 Answers

you can do it in two way

  1. add the Laravel built-in throttle middleware in route for example

     Route::post("/user/login","LoginController@login")->middleware("throttle:10,2");
    

it will allow 10 requests per 2 minute

  1. Use the Built-in Trait ThrottlesLogins

first, add ThrottlesLogins trait in the loginController and this line in the login method

if ($this->hasTooManyLoginAttempts($request)) {
    $this->fireLockoutEvent($request);
    return $this->sendLockoutResponse($request);
}

if(attempt()) {
    $this->clearLoginAttempts($request);
}else {
  $this->incrementLoginAttempts($request);
}

if attempt successfully then add this line in the attempt method

$this->clearLoginAttempts($request);

else fail login then add this line in else condition

$this->incrementLoginAttempts($request);

like image 156
Jignesh Joisar Avatar answered Nov 02 '22 02:11

Jignesh Joisar