How To Check Plain Text Matches Hashed Password In Laravel

  Apr 2024
  ITSolutionsGuides
  Category: Laravel

Welcome To ITSolutionsGuides,

Today Lets see How To Check Plain Text Matches Hashed Password In Laravel. Laravel uses Hasing technique for storing the passwords which is a one way hashing technique ie; it cannot be dehashed to the plain text. Since the passwords are stored in the Hashed format during the login attempt by the user we need to check the password entered by the user is correct or not by checking or matching with the Hashed password stored in the database. The password entered by the user will be a plain text format so we need to check or compare the plain text with the Hashed password. Laravel provides Hash::check() method to check the plain text and the Hashed password. Lets see how to use Hash::check() with a example,

Let's Start Coding

Lets create the PasswordController using the following artisan command,
php artisan make:controller PasswordController
In the controller we will check the plain text entered by the user matches the hashed password using the following syntax,

Hash::check(normal_password,hashed_password);

<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
 
class PasswordController extends Controller
{   
    public function update(Request $request): RedirectResponse
    {
        $data = Auth::user()->password;
        if (Hash::check($request->current_password, $data)) {
        $user = Auth::user();
        $user->password = Hash::make($request->new_confirm_password);
        $user->save();
      
      return redirect()->route('dashboard');
    } else {
        return redirect()->back();
    }
    }
}

We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.

Subscribe for NewsLetter

Be the first to know about releases and tutorial news and solutions.

We care about your data in our privacy policy.

ITSolutionsGuides

ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.

Contact US

ITSolutionsGuides, provide users with an easy-to-use form to reach out for support or inquiries.

whatsapp  gmail  instagram-new--v1  facebook-circled  twitter-circled  linkedin  github  pinterest 

Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides