Laravel 9 How To Check Current Password Using Hash Tutorial

Reading Time: 4 minutes
1,722 Views

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 –

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.

Hash::check("some_password", "$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.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

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.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more