How to Change Password with Current Password Validation in Laravel

Today, we will learn to update password with checking old password in laravel application. we will engender function to transmute password with old password validation rule in laravel. we will do old password verification utilizing custom validation rule in laravel 6, laravel 7 and laravel 8.

In this tutorial, we will engender example from starch. first we will engender auth then we engender change password page. after that we will engender our custom validation rules for checking with current password on database. then we utilize that custom validation rules on our controller file.

So, you require to just follow few step to get consummate guide for validate old password utilizing custom validation in laravel. you can visually perceive bellow annex screen shot to layout of our example:

Step 1 : Install Laravel

first of all we need to get fresh Laravel 8 version application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog

Step 2: Create Custom Validation Rule

After you can setup for migration and basic auth. i didn't write here for run migration and basic auth because you know how to do that. now we need to create new validation rules using following command:

php artisan make:rule MatchOldPassword

After this we can see they created new "Rules" folder with one file on app directory. you can update on that file like this way:

app/Rules/MatchOldPassword.php

<?php
  
namespace App\Rules;
  
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Facades\Hash;
  
class MatchOldPassword implements Rule
{
    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        return Hash::check($value, auth()->user()->password);
    }
   
    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'The :attribute is match with old password.';
    }
}

Step 3: Create Routes

Here, we need to add two route for view and another for post route. so open your "routes/web.php" file and add following route.

routes/web.php

use App\Http\Controllers\ChangePasswordController;

Route::get('change-password', [ChangePasswordController::class, 'index']);
Route::post('change-password', [ChangePasswordController::class, 'store'])->name('change.password');

Step 4: Create New Controller

In this step, now we should create new controller as ChangePasswordController. In this file we will create two method index() and store(). we will also use validation here. so let's do.

app/Http/Controllers/ChangePasswordController.php

<?php
   
namespace App\Http\Controllers;
   
use Illuminate\Http\Request;
use App\Rules\MatchOldPassword;
use Illuminate\Support\Facades\Hash;
use App\Models\User;
  
class ChangePasswordController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }
   
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        return view('changePassword');
    } 
   
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function store(Request $request)
    {
        $request->validate([
            'current_password' => ['required', new MatchOldPassword],
            'new_password' => ['required'],
            'new_confirm_password' => ['same:new_password'],
        ]);
   
        User::find(auth()->user()->id)->update(['password'=> Hash::make($request->new_password)]);
   
        dd('Password change successfully.');
    }
}

Step 5: Create Blade File

In this step, we need to create changePassword blade file and we will write code for form create and display error messages. so let's create file and put bellow code:

resources/views/changePassword.blade.php

@extends('layouts.app')
@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Change Password with Current Password Validation Example - HackTtheStuff</div>
   
                <div class="card-body">
                    <form method="POST" action="{{ route('change.password') }}">
                        @csrf 
   
                         @foreach ($errors->all() as $error)
                            <p class="text-danger">{{ $error }}</p>
                         @endforeach 
  
                        <div class="form-group row">
                            <label for="password" class="col-md-4 col-form-label text-md-right">Current Password</label>
  
                            <div class="col-md-6">
                                <input id="password" type="password" class="form-control" name="current_password" autocomplete="current-password">
                            </div>
                        </div>
  
                        <div class="form-group row">
                            <label for="password" class="col-md-4 col-form-label text-md-right">New Password</label>
  
                            <div class="col-md-6">
                                <input id="new_password" type="password" class="form-control" name="new_password" autocomplete="current-password">
                            </div>
                        </div>
  
                        <div class="form-group row">
                            <label for="password" class="col-md-4 col-form-label text-md-right">New Confirm Password</label>
    
                            <div class="col-md-6">
                                <input id="new_confirm_password" type="password" class="form-control" name="new_confirm_password" autocomplete="current-password">
                            </div>
                        </div>
   
                        <div class="form-group row mb-0">
                            <div class="col-md-8 offset-md-4">
                                <button type="submit" class="btn btn-primary">
                                    Update Password
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Now we are ready to run our example so run bellow command so quick run:

php artisan serve

Now you can open bellow url on your browser:

http://localhost:8000/

I hope it can help you...

Tags:

Was this article helpful?

0 out of 0 person found this article helpful.

Leave a comment

Or

No Comment