Skip to content
Advertisement

How to validate password to check if it is the same password as in database?

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

JavaScript

login.blade.php

JavaScript

Advertisement

Answer

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:

JavaScript

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

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