Category : #laravel
Tags : #laravel, #laravel auth
In this example, I will show you how to change password in laravel 9. We will change user password checking old password. If user provides previous password for updating new password, then they won't be allowed to do that. Cause, previous password and new password has to be same before updating the password.
So if you do not know how to create update password system in laravel 9 then this example is going to be for you. I will show you update password system with validation error message. So let's start the how to change user password in laravel 9.
Step 1: Install Laravel
First of all, we need to get a fresh Laravel 9 version application using the bellow command, So open your terminal OR command prompt and run the bellow command to start Laravel 9 update password system.
composer create-project laravel/laravel example-app
Read also: Laravel 9 Multi Auth Without User Model Using Guard
Step 2: Connect Database
We need registration, so need a database table to save user data. We will open the ".env" file and change the database name, username and password in the env file and create change password laravel 9.
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Enter_Your_Database_Name
DB_USERNAME=Enter_Your_Database_Username
DB_PASSWORD=Enter_Your_Database_Password
Step 3: Setup route
Now in this step, we have to create our route for how to change user password in laravel. Let's create our route.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\ThemeController;
use App\Http\Controllers\Admin\SettingsController;
Route::get('change/password',[SettingsController::class,'updatePasswordForm'])
->name('update.password')
->middleware('auth');
Route::post('change/password',[SettingsController::class,'updatePassword'])
->middleware('auth');
Step 4: Create Controller
Now in this step, we need to create our login controller and method which are defined in the doctor.php route. So let's create that method.
App\Http\Controllers\Admin\SettingsController.php
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use App\Models\User;
class SettingsController extends Controller
{
public function updatePasswordForm()
{
return view('welcome');
}
public function updatePassword(Request $request)
{
$request->validate([
'old_password' => 'required',
'new_password' => 'min:6|required_with:password_confirmation|same:password_confirmation',
'password_confirmation' => 'min:6'
]);
if(Hash::check($request->old_password , auth()->user()->password)) {
if(!Hash::check($request->new_password , auth()->user()->password)) {
$user = User::find(auth()->id());
$user->update([
'password' => bcrypt($request->new_password)
]);
session()->flash('message','Password updated successfully!');
return redirect()->back();
}
session()->flash('message','New password can not be the old password!');
return redirect()->back();
}
session()->flash('message','Old password does not matched!');
return redirect()->back();
}
}
Step 5: Create Blade File
Now we have to create a welcome.blade.php file. So create it and update it with the below code:
resourses\views\welcome.blade.php
@extends('admin.layouts.master')
@section('title') {{ trans('sentence.update_password')}} @endsection
@section('main')
<h4 class="fw-bold py-3 mb-4">{{ trans('sentence.update_password')}}</h4>
<div class="alert alert-warning fa-window-close" role="alert">
<h6 class="alert-heading fw-bold mb-2">{{ trans('sentence.warning')}}</h6>
<p class="mb-0">{{ trans('sentence.warning_password')}}</p>
</div>
<x-alert.alert-component />
<div class="card">
<div class="card-body">
<form
action="{{ route('update.password') }}"
method="post"
class="needs-validation"
role="form"
novalidate
>
@csrf
<div class="row">
<div class="col mb-3">
<label for="nameBasic" class="form-label">{{ trans('sentence.pre_pass')}}</label>
<input
type="password"
name="old_password"
class="form-control"
placeholder="{{ trans('sentence.pre_pass')}}"
required
>
<div class="invalid-tooltip">{{ trans('sentence.required')}}</div>
</div>
<div class="col mb-3">
<label for="nameBasic" class="form-label">{{ trans('sentence.new_pass')}}</label>
<input
type="password"
name="new_password"
class="form-control"
placeholder="{{ trans('sentence.new_pass')}}"
required
>
<div class="invalid-tooltip">{{ trans('sentence.required')}}</div>
</div>
<div class="col mb-3">
<label for="nameBasic" class="form-label">{{ trans('sentence.confirm_pass')}}</label>
<input
type="password"
name="password_confirmation"
class="form-control"
placeholder="{{ trans('sentence.confirm_pass')}}"
required
>
<div class="invalid-tooltip">{{ trans('sentence.required')}}</div>
</div>
</div>
<div class="fw-bold py-1 mt-3">
<button type="submit" class="button-create">{{ trans('sentence.save_changes')}}</button>
</div>
</div>
</div>
@endsection
Now all are set to go to check the custom password update system in laravel. So now you can test your app to check change password laravel. Hope it will help you.
Now you can test our application by visiting the below URL:
URL
Read also: Laravel 9 Custom Email Verification With Activation Code
Conclusion
Now we know update password with validate current password laravel. Hope this change password laravel 9 tutorial will help you to create old password validation laravel.