Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.5 Login errors not showing up

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.

like image 421
funerr Avatar asked Nov 03 '17 21:11

funerr


3 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
like image 67
Seva Kalashnikov Avatar answered Sep 24 '22 17:09

Seva Kalashnikov


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.

like image 35
funerr Avatar answered Sep 23 '22 17:09

funerr


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.

like image 39
Akshay Kulkarni Avatar answered Sep 24 '22 17:09

Akshay Kulkarni