Problem with displaying errors on login form using sessions

All we need is an easy explanation of the problem, so here it is.

I have login form in Laravel and I am having trouble displaying errors on my email and password fields. I made validator in which I have that error message and I put it in session and redirected it on GET /login. I need help on how to read those values from session and return them in blade as parameter and then to be able to display those error messages for email and password. Currently validation works but it displays no messages, it only redirects back to login page. Any help is appreciated. Here is my code.

web.php

Route::post('/login', 'Auth\[email protected]')->name('login');

LoginController

public function showLoginForm(Request $request)
{
    $session = $request->session()->get('data');

    return view('auth.login', compact('session'));
}

public function login(Request $request)
{
    $rules = array(
        'email'    => 'required|email', 
        'password' => 'required|alphaNum|min:3'
    );

    $validator = Validator::make($request->all(), $rules);

    if ($validator->fails()) {
        $request->session()->put('data', $request->input());
        return redirect()->intended('login')
            ->withErrors($validator)
            ->withInput($request->session()->put('data', $request->input()));
    } else {
        $userData = array(
            'email'     => $request->get('email'),
            'password'  => $request->get('password')
        );
    }
    
    if (Auth::attempt($userData)) {
        return redirect()->intended('dashboard');
    } else {        
        redirect()->intended('login');
    }
}

login blade

<div class="login-page">
        <div class="login-box">
            <div class="card mb-0">
                <div class="card-body login-card-body">
                    <p class="login-box-msg font-weight-bold">Sign in to start your session</p>
                    <form method="POST" class="mb-4" action="{{route('login') }}">
                        @csrf
                        <div class="input-group mb-3">
                            <input id="email" type="email" placeholder="Email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" autocomplete="email" autofocus>
                            <div class="input-group-append">
                                <div class="input-group-text">
                                    <span class="fas fa-user"></span>
                                </div>
                            </div>
                            @error('email')
                            <span class="invalid-feedback" role="alert">
                                <strong>{{ $message }}</strong>
                            </span>
                            @enderror
                        </div>
                        <div class="input-group mb-3">
                            <input id="password" type="password" placeholder="Password" class="form-control @error('password') is-invalid @enderror" name="password" autocomplete="current-password">
                            <div class="input-group-append">
                                <div class="input-group-text">
                                    <span class="fas fa-eye-slash cursor-pointer" style="display: none" onclick="showPassword()"></span>
                                    <span class="fas fa-eye cursor-pointer" onclick="showPassword()"></span>
                                </div>
                            </div>
                            @error('password')
                            <span class="invalid-feedback" role="alert">
                                <strong>{{ $message }}</strong>
                            </span>
                            @enderror
                        </div>
                        <div class="float-right">
                            <button type="submit" class="btn btn-primary btn-block font-weight-bold">Sign In</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You will have to call ->withErrors($validator->errors()). This returns the correct MessageBag. Otherwise you are returning a Validator instance.
Afterwards you can use the $errors variable or the @error tag as intended.

@edit
insted of inteded() use route() or back().

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply