Search code examples
phplaravelauthenticationhttp-redirectlaravel-5.3

Laravel 5.3 - Failed login attempt redirect


In my application I'm using the default authentication with Laravel 5.3. I have two places where a user can log in and I'm wanting to, in the event of a failed login, redirect to only one of those two places. In other words, whatever view a user logs in through, if they submit invalid credentials, they will always be redirected back to one specific view instead of whatever view they submitted the form from. In Laravel 5.1 it looks like this could be achieved by including the $loginPath variable in the login controller. In Laravel 5.3 they seem to have taken that option out of the documentation so I'm not sure how to approach this anymore.

Any thoughts and/or advice is greatly appreciated. Thanks!


Solution

  • Edit: I misunderstood your original question. This is updated.

    If you need to customize this, you can do something like:

    Open App\Http\Controllers\Auth\LoginController (as per the docs, this would have been generated by the php artian make:auth command thay I'm assuming you used) and add this:

    /**
     * Get the failed login response instance.
     *
     * @param \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    protected function sendFailedLoginResponse(Request $request)
    {
        return redirect()->to('/the_redirect_location')
            ->withInput($request->only($this->username(), 'remember'))
            ->withErrors([
                $this->username() => Lang::get('auth.failed'),
            ]);
    }
    

    This will overwrite the same method that's contained in \Illuminate\Foundation\Auth\AuthenticatesUsers trait that LoginController uses. The redirect()->to('/the_redirect_location') is the part I've changed. Originally, it's: redirect()->back().

    If you choose to use this method, be sure to add this at the top of LoginController:

    use Lang;
    use Illuminate\Http\Request;