Laravel hashed password with bcrypt
algorithm is not decryptable and to match the hashed string with plain string we use Hash::check
method. For hashing the password laravel use secure Bcrypt
and Argon2 hashing for storing user passwords.
So in this tutorial i will show you to check password with hashed string store in database for login purpose or validating the current password.
In some cases we need to check our password from database which hashed but we cannot match directly it using equals to operator because stored password is hashed and user input password is plain string so to verify the entered password and stored password is same we use Hash::check
method which accepts two parameters first is plain string and second is hashed password string of database.
Here is the syntax of laravel match password
Hash::check($plain_string,$hashed_password_string)
the above code will match the plain string and hashed bcrypt
string.
Example to match the plain string with hash string
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class UserController extends Controller
{
/**
* Show the form to create a new blog post.
*
* @return \Illuminate\View\View
*/
public function login(Request $request)
{
$user = User::where("email",$request->email)->first();
if(Hash::check($request->password,$user->password)){
echo "password match, you can now start session";
}
return "DONE";
}
Also read : How to create hash password in laravel 8 ?