0

I have login form in Laravel that uses email and password to log on site. I have all validation and everything works fine except for password. When I type wrong password it goes to blank page and I want to write some error beneath password field. I looked in same:password validation but it doesn't work. Any help is appreciated. Here is my code.

LoginController.php

public function login(Request $request)
{
    $rules = [
        'email'    => 'required|email|exists:App\User,email', 
        'password' => 'required|alphaNum|min:5'
    ];

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

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

login.blade.php

<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') }}" required 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" required 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>
14
  • Can you add the code for your login view too? Commented Aug 7, 2020 at 14:05
  • @Unflux Here it is Commented Aug 7, 2020 at 14:09
  • 1
    You need to hash password before storing. Commented Aug 7, 2020 at 14:11
  • why are you trying to validate the password against the database in the first place? just so you can know if they failed the login because of 'email' or 'password'? Commented Aug 7, 2020 at 14:44
  • 1
    so now an attacker will know that an email address exists (is real) and can now focus on the password ;-) but that may or may not be a worry for you, just pointing that out Commented Aug 7, 2020 at 14:54

1 Answer 1

3

Assuming that your email is unique, you first have to get the DB row where the $request->email is: $user = User::where('email', $request->email)->first()

You can then check it by using: Hash::check($request->password, $user->password)

https://laravel.com/docs/7.x/hashing

@edit

To add it to the rules you will have to create a Rule Class: php artisan make:rule myRuleName Afterwards you will call it like that:

$rules = [
        'email'    => 'required|email|exists:App\User,email', 
        'password' => ['required','alphaNum','min:5', new myRuleName()],
    ];

In your custom Rule Class you will find a passes($attribute, $value)function. Now you can insert the code i wrote above into this method. You will have to replace $request->password with $value

Sign up to request clarification or add additional context in comments.

4 Comments

I am having trouble with writing the code. How should I add this to the $rules variable?
I get only redirect to login page no message
I am not sure why the error is not displayed. But you will need to post another question because it is another problem.
I will in a minute

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.