Laravel - Change Password with Current Password Validation Example

Laravel - Change Password with Current Password Validation Example

Step 1: Install Laravel 8

Start by creating a new Laravel project:

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

Step 2: Install Laravel UI (with Vue Auth)

Install Laravel UI and scaffold the authentication system:

composer require laravel/ui php artisan ui vue --auth npm install && npm run dev

 Step 3: Update Environment Configuration

Open your .env File and configure your database credentials:

DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=your_db DB_USERNAME=root DB_PASSWORD=your_password

For sending password reset emails:

MAIL_MAILER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=your_email@gmail.com MAIL_PASSWORD=your_email_password MAIL_ENCRYPTION=tls

Step 4: Run Migrations

Create necessary database tables:

php artisan migrate

Step 5: Create Change Password Blade View

Create the view file:
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"> <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"> </div> </div> <div class="form-group row mt-3"> <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"> </div> </div> <div class="form-group row mt-3"> <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"> </div> </div> <div class="form-group row mt-4"> <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

Step 6: Create a Custom Rule to Match Old Password

Create a rule:

php artisan make:rule MatchOldPassword

Edit app/Rules/MatchOldPassword.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 current password does not match our records.'; } }

Step 7: Create ChangePasswordController

Generate controller:

php artisan make:controller ChangePasswordController

Then edit app/Http/Controllers/ChangePasswordController.php:

namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Rules\MatchOldPassword; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Auth; 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'], 'new_confirm_password' => ['same:new_password'], ]); Auth::user()->update(['password' => Hash::make($request->new_password)]); return back()->with('status', 'Password changed successfully!'); } }

Step 8: Register Routes

In routes/web.php, add:

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

Step 9: Run Development Server

Launch the Laravel dev server:

php artisan serve

Open your browser and go to:

http://localhost:8000/change-password
Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close