Add toomanyattempts in my login controller in laravel

153 Views Asked by At

I need to add the functionality that toomanyloginattempts with my login . now its not working. Iam using Laravel Framework 5.1.45 (LTS). The code that i used is mentioned below. My controller function is

    <?php
    use App\Libraries\SessionHelper;
    use App\Libraries\ConfigUtils;
    use App\Libraries\GeneralLib;
    use App\Models\OrgSettings;
    use App\Http\Controllers\Controller;
    use Illuminate\Foundation\Auth\ThrottlesLogins;
    use Illuminate\Foundation\Auth\AuthenticatesUsers;
    use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

    class LoginController extends Controller {
     use AuthenticatesAndRegistersUsers, ThrottlesLogins;

      public function doLogin() {
        $email = Input::get('email');
        $pass = Input::get('password');
        $candidate_login_user = User::getUserByEmail($email);
        $data = User::authenticate($email, $pass);
        if (empty($data)) {
          User::logFailedAuthentication($email, $candidate_login_user->organization);
          Session::flash('error', "Incorrect email or password.");
          return Redirect::to('/login');
        }

    }

my view page is as follows

    <form action="login" method="post">
                    <div class="body bg-gray">
                       <div class="alert alert-danger">
        <strong >Whoops!</strong> There were some problems with your input.<br><br>
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

                        <?php
                            Session::forget('error');
                            Session::forget('success');
                        ?>
                        <div class="form-group">
                            <input type="email" name="email" class="form-control"
                                placeholder="email"/>
                        </div>
                        <div class="form-group">
                            <input type="password" name="password"
                                class="form-control" placeholder="password"/>
                        </div>
1

There are 1 best solutions below

1
On

Since you're implementing your own login action it is not enough to just add the traits to your LoginController to implement throttling.

You need to be checking the hasTooManyLoginAttempts method from your doLogin action, and firing the lockout event yourself, if necessary.

public function doLogin(\Illuminate\Http\Request $request) {
    if ($this->hasTooManyLoginAttempts($request)) {
        $this->fireLockoutEvent($request);

        return $this->sendLockoutResponse($request);
    }


    $email = Input::get('email');
    $pass = Input::get('password');
    $candidate_login_user = User::getUserByEmail($email);
    $data = User::authenticate($email, $pass);


    if (empty($data)) {
        User::logFailedAuthentication($email, $candidate_login_user->organization);
        Session::flash('error', "Incorrect email or password.");
        $this->incrementLoginAttempts($request);
        return Redirect::to('/login');
    }
}

Altogether, I think you'd be better off simply using the built-in Auth controllers to handle your login (or at least using them as a starting point) rather than reimplement your own.