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.
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With