Inside this article we will see the concept i.e Laravel 9 How To Check Current Password Using Hash Tutorial. Article contains the classified information about checking a plain text string value to a hashed value in laravel.
Laravel provides a facade class i.e Hash. This Hash facade class has a static method called check(). This method you can use to compare a plain text value with a hashed value.
Hash::check()
check() method of Hash facade class returns a boolean value.
Learn More –
- Laravel 9 How To Use Ternary Operator in Blade Templates
- Laravel 9 Image Upload with Tailwind CSS Tutorial
- Laravel 9 How To Get All Models From Application Tutorial
- Laravel 9 How to Make Hash Password Tutorial
Let’s get started.
Laravel Installation
Open terminal and run this command to create a laravel project.
composer create-project laravel/laravel myblog
It will create a project folder with name myblog inside your local system.
To start the development server of laravel –
php artisan serve
URL: http://127.0.0.1:8000
Assuming laravel already installed inside your system.
Generate Hash Password Using Hash
We will use Facade Hash class to generate hashed password. We will make use of make() a static method from it.
Hash Facade is linked from here:
use Illuminate\Support\Facades\Hash;
Open any of controller from your application.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
class CustomController extends Controller
{
/**
* Write Your Code..
*
* @return string
*/
public function store(Request $request)
{
$password = Hash::make("Sanjay Kumar");
dd($password);
}
}
We will get output this hashed password –
$2y$10$xtvy7GbXW.DnlP79tmjiQuS6ra1r132vTAcyQvehEeWYDiDZqGr0a
User Password Check with Hash
We will see password check concept between a plain text value and a hashed value.
Let’s say you have this controller. Implement the concept here,
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\Http\Controllers\Controller;
use App\Models\User;
class PasswordController extends Controller
{
/**
* Updating the password for the user.
*
* @param Request $request
* @return Response
*/
public function update(Request $request)
{
$input = $request->all();
// User data from database
$user = User::find(auth()->user()->id);
// Password check
if (!Hash::check($input['current_password'], $user->password)) {
// Password didn't match
} else {
// Code to update your old password
}
}
}
Concept
Hash::check() returns a boolean value after check. False when value didn’t match and True when values match.
Hash::check($input['current_password'], $user->password)
This value $input[‘current_password’] is a plain text value whereas $user->password is a hashed password value.
<meta http-equiv="content-type" content="text/html; charset=utf-8">Hash::check("some_password", "<meta http-equiv="content-type" content="text/html; charset=utf-8">$2y$10$xtvy7GbXW.DnlP79tmjiQuS6ra1r132vTAcyQveh895YDiDZqGr0a")
We hope this article helped you to Learn Laravel 9 How To Check Current Password Using Hash Tutorial in a very detailed way.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.
Read more