Limit login attempts in Laravel 5.2

Viewed 8206

I am created a Laravel 5.2 application; I need to limit failure login attempts.I am created a AuthController with following code; But not working logging attempt lock.

<?php

 namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use Auth;
use URL;

 use Illuminate\Http\Request;
 use App\Http\Controllers\Controller;
 use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{


use AuthenticatesAndRegistersUsers;

   protected $maxLoginAttempts=5;
   protected $lockoutTime=300;


/**
 * Create a new authentication controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest', ['except' => 'getLogout']);
    $this->loginPath = URL::route('login');
    $this->redirectTo = URL::route('dashboard'); //url after login
    $this->redirectAfterLogout = URL::route('home');
}

public function index()
{
    return 'Login Page';
}


/**
 * Handle a login request to the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function postLogin(Request $request)
{
    $this->validate($request, [
        'username' => 'required', 'password' => 'required',
    ]);


    $throttles = $this->isUsingThrottlesLoginsTrait();

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

    $credentials = $this->getCredentials($request);

    if (Auth::attempt($credentials, $request->has('remember'))) {
        return redirect()->intended($this->redirectPath());
    }



    if ($throttles) {
        $this->incrementLoginAttempts($request);
    }


    return redirect($this->loginPath)
        ->withInput($request->only('username', 'remember'))
        ->withErrors([
            'username' => $this->getFailedLoginMessage(),
        ]);
}

/**
 * Get the needed authorization credentials from the request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
protected function getCredentials(Request $request)
{
    return $request->only('username', 'password');
}


/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return User
 */
protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'username' => $data['username'],
        'password' => bcrypt($data['password']),
    ]);
  }
}

After many failure login their is no error message displayed. I am added some line to display error in login.blade.php file

4 Answers

Just overriding the following 2 functions maxAttempts and decayMinutes will be good to go. This 2 functions belong to Illuminate\Foundation\Auth\ThrottlesLogins.php file. I have tested on Laravel 5.6 version and working fine.

public function maxAttempts()
{
    //Lock on 4th Failed Login Attempt
    return 3;
}

public function decayMinutes()
{
    //Lock for 2 minutes
    return 2;
}
Related