Change Password with Current Password Laravel

Change Password with Current Password Laravel

Laravel: Change Password with Current Password Validation

This guide demonstrates how to create a secure password change feature in Laravel, requiring users to confirm their current password before setting a new one.

Step 1: Install Laravel

Create a new Laravel project using Composer:

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

Step 2: Create a Custom Validation Rule

Generate a rule class to validate the old password:

php artisan make:rule MatchOldPassword

Edit the generated file at app/Rules/MatchOldPassword.php:

<?php namespace App\Rules; use Illuminate\Contracts\Validation\Rule; use Illuminate\Support\Facades\Hash; class MatchOldPassword implements Rule { public function passes($attribute, $value) { return Hash::check($value, auth()->user()->password); } public function message() { return 'The :attribute does not match your current password.'; } }

Step 3: Define Routes

Open routes/web.php and add the following routes:

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 the Controller

Generate and update 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 { public function __construct() { $this->middleware('auth'); } public function index() { return view('changePassword'); } public function store(Request $request) { $request->validate([ 'current_password' => ['required', new MatchOldPassword], 'new_password' => ['required', 'min:8'], 'new_confirm_password' => ['same:new_password'], ]); User::find(auth()->id())->update([ 'password' => Hash::make($request->new_password), ]); return back()->with('success', 'Password changed successfully.'); } }

Step 5: Create Blade View

Create a Blade file at 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</div> <div class="card-body"> @if (session('success')) <div class="alert alert-success">{{ session('success') }}</div> @endif <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 class="col-md-4 col-form-label text-md-right">Current Password</label> <div class="col-md-6"> <input type="password" class="form-control" name="current_password" required> </div> </div> <div class="form-group row"> <label class="col-md-4 col-form-label text-md-right">New Password</label> <div class="col-md-6"> <input type="password" class="form-control" name="new_password" required> </div> </div> <div class="form-group row"> <label class="col-md-4 col-form-label text-md-right">Confirm New Password</label> <div class="col-md-6"> <input type="password" class="form-control" name="new_confirm_password" required> </div> </div> <div class="form-group row mb-0 mt-3"> <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

Final Step: Run the Laravel Development Server

php artisan serve

Visit your application in the browser:

🔗 http://localhost:8000/change-password

Souy Soeng

Souy Soeng

Hi there 👋, I’m Soeng Souy (StarCode Kh)
-------------------------------------------
🌱 I’m currently creating a sample Laravel and React Vue Livewire
👯 I’m looking to collaborate on open-source PHP & JavaScript projects
💬 Ask me about Laravel, MySQL, or Flutter
⚡ Fun fact: I love turning ☕️ into code!

Post a Comment

CAN FEEDBACK
close