Nistha Adhikari
Nistha Adhikari

Reputation: 147

Hash::check() always returns false

I am trying to check the user input password while logging in and the Hashed password in the database, but it always returns false.

This is the code in the controller Hashing the password

$doctor = new doctor;
    $doctor->name = $request->name;
    $doctor->address = $request->address;
    $doctor->email = $request->email;
    $doctor->mobile = $request->mobile;
    $password = Hash::make($request->doctorPassword);
    $doctor->doctorPassword = $password;

This is code for validating the password: $data = $request->all();

    $user = doctor::where('email',$data['doctorUsername'])->first();
    if ($user) {

        if (Hash::check($user->doctorPassword, $data['doctorPassword']))
        {
            return view($this->filePath . '.doctorProfile');
        }

}

Always returns false.

Upvotes: 2

Views: 1266

Answers (2)

Twaha Mukammel
Twaha Mukammel

Reputation: 31

I think everything is ok here. Just place the hashedPassword $user->doctorPassword as the second argument in Hash::check(). Cheers :D

$user = doctor::where('email',$data['doctorUsername'])->first();
    if ($user) {

        if (Hash::check($data['doctorPassword'], $user->doctorPassword))
        {
            return view($this->filePath . '.doctorProfile');
        }

Please check this link: https://laravel.com/docs/5.8/hashing

Upvotes: 0

thisiskelvin
thisiskelvin

Reputation: 4202

Your argument order is incorrect. You should be adding the plain text version of the password first like so:

Hash::check($data['doctorPassword'], $user->doctorPassword);

Upvotes: 6

Related Questions