Laravel - Change Password with Current Password Validation Example
Whatsapp
https://fundaofwebit.com/post/laravel-change-password-with-current-password-validation-example
How to Change Password with Current Password Validation Example
In this post, you will be learning how to change password with existing / current password validation in laravel.
Custom password change option in laravel.
Step 1: Install Laravel application and migrate and complete the basic setup of laravel.
Step 2: Create a route in routes/web.php file
Route::get('change-password', [App\Http\Controllers\ChangePasswordController::class, 'index']);
Route::post('change-password', [App\Http\Controllers\ChangePasswordController::class, 'changePassword']);
Step 3: Create a controller named ChangePasswordController as follows:
php artisan make:controller ChangePasswordController
Paste the below code in created ChangePasswordController.php
( app/Http/Controllers/ChangePasswordController.php )
<?php
namespace App\Http\Controllers\Frontend;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class ChangePasswordController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('change-password');
}
public function changePassword(Request $request)
{
$request->validate([
'current_password' => ['required','string','min:8'],
'password' => ['required', 'string', 'min:8', 'confirmed']
]);
$currentPasswordStatus = Hash::check($request->current_password, auth()->user()->password);
if($currentPasswordStatus){
User::findOrFail(Auth::user()->id)->update([
'password' => Hash::make($request->password),
]);
return redirect()->back()->with('message','Password Updated Successfully');
}else{
return redirect()->back()->with('message','Current Password does not match with Old Password');
}
}
}
Step 4: Create a Blade file named change-password.blade.php in
( resources/views/change-password.blade.php ):
@extends('layouts.app')
@section('content')
<div class="py-5">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-6">
@if (session('message'))
<h5 class="alert alert-success mb-2">{{ session('message') }}</h5>
@endif
@if ($errors->any())
<ul class="alert alert-danger">
@foreach ($errors->all() as $error)
<li class="text-danger">{{ $error }}</li>
@endforeach
</ul>
@endif
<div class="card shadow">
<div class="card-header bg-primary">
<h4 class="mb-0 text-white">Change Password</h4>
</div>
<div class="card-body">
<form action="{{ url('change-password') }}" method="POST">
@csrf
<div class="mb-3">
<label>Current Password</label>
<input type="password" name="current_password" class="form-control" />
</div>
<div class="mb-3">
<label>New Password</label>
<input type="password" name="password" class="form-control" />
</div>
<div class="mb-3">
<label>Confirm Password</label>
<input type="password" name="password_confirmation" class="form-control" />
</div>
<div class="mb-3 text-end">
<hr>
<button type="submit" class="btn btn-primary">Update Password</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
Now you are ready to run this application for password change in laravel.
Thanks for reading.
© Copyright - 2018 - 2023 | All rights reserved at Funda of Web IT