diegoronan's avatar

Laravel 5.3 authentication error message

Hi everyone. I'd like to know why the password error message it's not showing up on Laravel 5.3. Only in the email field is working.

0 likes
6 replies
leonardharley's avatar
Level 3

The LoginController uses the AuthenticatesUsers trait which has a method sendFailedLoginResponse which is responsible for the error message redirect upon authentication failure as follows:

protected function sendFailedLoginResponse(Request $request)
    {
        return redirect()->back()
            ->withInput($request->only($this->username(), 'remember'))
            ->withErrors([
                $this->username() => Lang::get('auth.failed'),
            ]);
    }

As can be seen above the error message redirect is only linked to the email address field on the login form and the value contained in the auth.failed language file which is why it will always show the default error message under the email address.

To get specifc error messages for either invalid email or incorrect password (which is what I am assuming you want to do), you can override the sendFailedLoginResponse method in your LoginController. Something like the following should do the trick:

     * Method override to send correct error messages
     * Get the failed login response instance.
     *
     * @param \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    protected function sendFailedLoginResponse(Request $request)
    {

        if ( ! User::where('email', $request->email)->first() ) {
            return redirect()->back()
                ->withInput($request->only($this->username(), 'remember'))
                ->withErrors([
                    $this->username() => Lang::get('auth.email'),
                ]);
        }

        if ( ! User::where('email', $request->email)->where('password', bcrypt($request->password))->first() ) {
            return redirect()->back()
                ->withInput($request->only($this->username(), 'remember'))
                ->withErrors([
                    'password' => Lang::get('auth.password'),
                ]);
        }

    }

You will also of course need to edit your language file (auth.php) to something simlar to the following:

return [
    /*
    |--------------------------------------------------------------------------
    | Authentication Language Lines
    |--------------------------------------------------------------------------
    |
    | The following language lines are used during authentication for various
    | messages that we need to display to the user. You are free to modify
    | these language lines according to your application's requirements.
    |
    */
    'email' => 'Email address not found',
    'password' => 'Incorrect password',
];
9 likes
cawecoy's avatar

It seems that the method sendFailedLoginResponse has changed in Laravel 5.5. I've overridden it like:

use Illuminate\Validation\ValidationException;

...

protected function sendFailedLoginResponse(Request $request)
{
    throw ValidationException::withMessages([
        $this->username() => [trans('auth.failed')],
    ])->redirectTo('login');
}
1 like
marcosdipaolo's avatar

If you see auth.failed probably you just need to create a folder for your locale at resources/lang and at the auth.php define the 'failed' key with the right translation

lyzah's avatar

In Laravel 6 the "Lang::get('auth.email')," was causing an error so I changed it to "[trans('auth.email')]," and @leonardharley's code worked as expected...sorry if this is maybe repeating @cawecoy comment somewhat.

Please or to participate in this conversation.