How to compare Laravel's hash password using a custom login form? How to compare Laravel's hash password using a custom login form? laravel laravel

How to compare Laravel's hash password using a custom login form?


First, you cannot do it this way. Assuming username is unique, you should do:

$validate_admin = DB::table('administrators')                            ->select('username')                            ->where('username', Input::get('admin_username'))                            ->first();if ($validate_admin && Hash::check(Input::get('admin_password'), $validate_admin->password)) {  // here you know data is valid}

However you should think about rather using built-in methods than coding it yourself. You have Auth::attempt or Auth::validate if you want to login/check only user with password so there's really no need to code it yourself.


Here you're checking the string 'password' with the hashed version of the input password.

So try fetching the user by their username and if you've a result you can compare the hashed version of the password, stored in the database, with the input password. Like so:

$user = DB::table('administrators')        ->select('username', 'password')        ->where('username', Input::get('admin_username');if($user->count()) {    $user = $user->first();    if(Hash::check(Input::get('admin_password'), $user->password)) {         //User has provided valid credentials :)    }}


A slight improvement to marcin-nabiaƂek's answer, you can now use PHP's password_verify to achieve the same

$user = App\User::where('email', $request->email)->first();if($user && password_verify($request->password, $user->password)) {   // authenticated user,   // do something...}