Laravel - Change Password with Current Password Validation Example

Laravel - Change Password with Current Password Validation Example

Today, we will learn to update password with checking old password in laravel 5 application. we will create function to change password with old password validation rule in laravel. we will do old password verification using custom validation rule in laravel 6, laravel 7 and laravel 8.

 

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

 

So, you need to just follow few step to get complete guide for validate old password using custom validation in laravel. you can see bellow attach screen shot to layout of our example:

 

Preview

 

 

Step 1 : Install Laravel 5.8

 

first of all we need to get fresh Laravel 5.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

  1. 
     
  2. <?php
  3.  
  4. namespace App\Rules;
  5.  
  6. use Illuminate\Contracts\Validation\Rule;
  7. use Illuminate\Support\Facades\Hash;
  8.  
  9. class MatchOldPassword implements Rule
  10. {
  11. /**
  12. * Determine if the validation rule passes.
  13. *
  14. * @param string $attribute
  15. * @param mixed $value
  16. * @return bool
  17. */
  18. public function passes($attribute, $value)
  19. {
  20. return Hash::check($value, auth()->user()->password);
  21. }
  22.  
  23. /**
  24. * Get the validation error message.
  25. *
  26. * @return string
  27. */
  28. public function message()
  29. {
  30. return 'The :attribute is match with old password.';
  31. }
  32. }

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

Route::get('change-password', 'ChangePasswordController@index');

Route::post('change-password', 'ChangePasswordController@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


 
  1. 
     
  2. <?php
  3.  
  4. namespace App\Http\Controllers;
  5.  
  6. use Illuminate\Http\Request;
  7. use App\Rules\MatchOldPassword;
  8. use Illuminate\Support\Facades\Hash;
  9. use App\User;
  10.  
  11. class ChangePasswordController extends Controller
  12. {
  13. /**
  14. * Create a new controller instance.
  15. *
  16. * @return void
  17. */
  18. public function __construct()
  19. {
  20. $this->middleware('auth');
  21. }
  22.  
  23. /**
  24. * Show the application dashboard.
  25. *
  26. * @return \Illuminate\Contracts\Support\Renderable
  27. */
  28. public function index()
  29. {
  30. return view('changePassword');
  31. }
  32.  
  33. /**
  34. * Show the application dashboard.
  35. *
  36. * @return \Illuminate\Contracts\Support\Renderable
  37. */
  38. public function store(Request $request)
  39. {
  40. $request->validate([
  41. 'current_password' => ['required', new MatchOldPassword],
  42. 'new_password' => ['required'],
  43. 'new_confirm_password' => ['same:new_password'],
  44. ]);
  45.  
  46. User::find(auth()->user()->id)->update(['password'=> Hash::make($request->new_password)]);
  47.  
  48. dd('Password change successfully.');
  49. }
  50. }

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


 
  1. @extends('layouts.app')
  2.  
  3. @section('content')
  4. <div class="container">
  5. <div class="row justify-content-center">
  6. <div class="col-md-8">
  7. <div class="card">
  8. <div class="card-header">Laravel - Change Password with Current Password Validation Example - ItSolutionStuff.com</div>
  9.  
  10. <div class="card-body">
  11. <form method="POST" action="{{ route('change.password') }}">
  12. @csrf
  13.  
  14. @foreach ($errors->all() as $error)
  15. <p class="text-danger">{{ $error }}</p>
  16. @endforeach
  17.  
  18. <div class="form-group row">
  19. <label for="password" class="col-md-4 col-form-label text-md-right">Current Password</label>
  20.  
  21. <div class="col-md-6">
  22. <input id="password" type="password" class="form-control" name="current_password" autocomplete="current-password">
  23. </div>
  24. </div>
  25.  
  26. <div class="form-group row">
  27. <label for="password" class="col-md-4 col-form-label text-md-right">New Password</label>
  28.  
  29. <div class="col-md-6">
  30. <input id="new_password" type="password" class="form-control" name="new_password" autocomplete="current-password">
  31. </div>
  32. </div>
  33.  
  34. <div class="form-group row">
  35. <label for="password" class="col-md-4 col-form-label text-md-right">New Confirm Password</label>
  36.  
  37. <div class="col-md-6">
  38. <input id="new_confirm_password" type="password" class="form-control" name="new_confirm_password" autocomplete="current-password">
  39. </div>
  40. </div>
  41.  
  42. <div class="form-group row mb-0">
  43. <div class="col-md-8 offset-md-4">
  44. <button type="submit" class="btn btn-primary">
  45. Update Password
  46. </button>
  47. </div>
  48. </div>
  49. </form>
  50. </div>
  51. </div>
  52. </div>
  53. </div>
  54. </div>
  55. @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...