Spread the love

Laravel provides robust security features and one of them are hash password which is not decryptable. For hashing the password laravel use secure Bcrypt and Argon2 hashing for storing user passwords. Password encrypted with Bcrypt can not be decrypt since it uses key to generate the hashed string and irreversible algorithm.

So in this tutorial i will show you to hashing a password string to store in database and check the password again for login purpose or validating the current password.

Here is the syntax and example

<?php 
use Illuminate\Support\Facades\Hash;
$password= "12345678";
Hash::make($password);

//or using the helper function bycrypt

bcrypt($password);

the above code will generate bcrypt string.

How to check current or database hash password in laravel

As i mentioned above its impossible to decrypt the password since its encrypted with bcrypt algorithm but we can match the plain password string with hashed password in laravel using Hash::check method.

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.

so simple syntax

Hash::check($request->password,$user->password)

Here is the example to use

<?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";
   }

Leave a Reply