Skip to content

Problem with displaying errors on login form using sessions

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', 'AuthLoginController@login')->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>

Advertisement

Answer

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().

User contributions licensed under: CC BY-SA
4 People found this is helpful