Laravel 5.5 Login errors not showing up

11,704

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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.

Share:
11,704
funerr
Author by

funerr

Hello! #SOreadytohelp Favorite quotes (regarding programming): “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” Martin Golding “Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” Rich Cook “Good code is its own best documentation. As you’re about to add a comment, ask yourself, ‘How can I improve the code so that this comment isn’t needed?’” Steve McConnell

Updated on June 17, 2022

Comments

  • funerr
    funerr almost 2 years

    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.

  • Seva Kalashnikov
    Seva Kalashnikov over 6 years
    Here's the example from Laravel Basic Auth Views: github.com/drbyte/laravel-basic-auth-views/blob/master/…
  • Ohgodwhy
    Ohgodwhy over 6 years
    and here's an example from the documentaion. it should be $errors->any(). the view message bag is always available.