Laravel 5.5 Login errors not showing up

Viewed 12058

Here is my login.blade.php

@if(Session::get('errors')||count( $errors ) > 0)
   @foreach ($errors->all() as $error)
      <h1>{{ $error }}</h1>
  @endforeach
@endif

Here is my LoginController.php:

protected function sendFailedLoginResponse(Request $request)
{
    return redirect()->back()
        ->withInput($request->only($this->username(), 'remember'))
        ->withErrors([
            $this->username() => 'ERRORS',
        ]);
}

And here is my web.php (routes)

// I am customizing the login to do extra checks, 
// but I still need the basic auth scaffolding.
Auth::routes(); 
...
Route::group(['middleware' => 'web'], function () {
  Route::view('/login', 'auth.login');
  Route::post('/login', 'Auth\LoginController@login')->name('login');
});

When I try to login with a bad user it shows no errors in the view, what am I doing wrong?

Update:
I've tried to change the login.blade.php, as @Seva Kalashnikov suggested, with no luck.
I've also tried @Akshay Kulkarni suggestion with no luck.

5 Answers

Try to remove Session::get('errors') from your if statement in login.blade.php

@if(count( $errors ) > 0)
    @foreach ($errors->all() as $error)
       <h1>{{ $error }}</h1>
    @endforeach
@endif

ShareErrorsFromSession middleware, which is provided by the web middleware group is responsible for $error view variable so it will always be defined (link here)

[UPDATE]

And as @Ohgodwhy pointed, you need to use @if ($errors->any()) Example

So in your case it will be:

@if($errors->any())
    @foreach ($errors->all() as $error)
       <h1>{{ $error }}</h1>
    @endforeach
@endif

Ok, after a few hours I finally found it! I created a Laravel project from scratch and made a diff to find the culprit:

In app/Http/Kernel.php, make sure to get rid of the StartSession middleware:

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
    \App\Http\Middleware\TrimStrings::class,
    \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    \App\Http\Middleware\TrustProxies::class,
    \Illuminate\Session\Middleware\StartSession::class, // <-- Remove this
];

Explanation: I had it there because I read that I had to put it as a middleware (if I wasn't using the Route::group(['middleware' =>'web'] wrapper in my web.php), I think that I forgot it there. I think that putting it there and using the wrapper in web.php somehow truncate the error session before it gets to the view.

Put,

Auth::routes();

Inside middleware group.

Web middleware starts the session. If you are writing any route outside that middleware group then you can not access the session.

If you use Entrust (or maybe some other packages) and add its classes to $routeMiddleware, the problem can be deriving from that your subsequently added custom classes override the default Laravel classes.

The solution is to move your custom classes to the top of $routeMiddleware array.

I faced same problem. After a lot of digging here and there, I solved it by removing \Illuminate\Session\Middleware\StartSession::class from $middlewareGroups of 'web' in app\http\kernel.php.

protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class, // <-- Remove this
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];

It is realted to @halfer's solution.

Related