Verify that Password Matches Hash in Laravel 9

Verify that Password Matches Hash in Laravel 9

When implementing authentication in a web application, we may need to check whether the user's password matches a given hash. This tutorial shows example how to verify that password matches hash in Laravel 9 application.

Laravel provides the Hash facade that has the check method which allows checking if the given plaintext password matches hash.

app/Http/Controllers/TestController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Response;
use Illuminate\Support\Facades\Hash;

class TestController extends Controller
{
    public function index(): Response
    {
        $hashedPassword = '$2y$10$sauTFphNj2jg5VXnZPOjbusPScf14E4sfEm6ns9SWdnLM7dmxG3XS';
        $plaintextPassword = 'pwd123';

        if (!Hash::check($plaintextPassword, $hashedPassword)) {
            return new Response('Invalid password');
        }

        return new Response('Valid password');
    }
}

Leave a Comment

Cancel reply

Your email address will not be published.