In this tutorial, we are going to learn how to implement Change password functionality in Laravel. When you need to change your password with submit your current password, this will be the easiest way to do it.

Prerequisites

  • Laravel
  • To Continue with this tutorial, you should have installed Laravel 8 in your pc. If you are still not installed Laravel in your machine you can configure it from here.

    Setting up the Project

    First you need to create a new laravel project by running below command in your terminal
    1
    composer create-project laravel/laravel laravel-change-password

    Then navigate to your project directory by using below command in your terminal

    1
    cd laravel-change-password

    Set up basic auth

    After creating our project, now you can set up default Laravel auth and run database migration.

    Create custom validation Rule

    Now we need to create a custom validation rule to validate our old password. You can create this custom rule by running:
    1
    php artisan make:rule MatchPassword

    After creating our custom rule, now you need to paste below code in it:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    <?php

    namespace App\Rules;

    use Illuminate\Contracts\Validation\Rule;
    use Illuminate\Support\Facades\Hash;

    class MatchPassword implements Rule
    {
    /**
    * Create a new rule instance.
    *
    * @return void
    */
    public function __construct()
    {
    //
    }

    /**
    * Determine if the validation rule passes.
    *
    * @param string $attribute
    * @param mixed $value
    * @return bool
    */
    public function passes($attribute, $value)
    {
    return Hash::check($value, auth()->user()->password);
    }

    /**
    * Get the validation error message.
    *
    * @return string
    */
    public function message()
    {
    return 'The :attribute should match with old password.';
    }
    }

    Create ChangePasswordController

    Now we need to create need to implement two functions within our controller. First function for showing our change password view and second for change password functionality. You can create your controller using following command:
    1
    php artisan make:controller ChangePasswordController

    After that you paste below code in it:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    <?php

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use App\Rules\MatchPassword;
    use Illuminate\Support\Facades\Hash;
    use App\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 MatchPassword],
    'new_password' => ['required'],
    'new_confirm_password' => ['same:new_password'],
    ]);

    User::find(auth()->user()->id)->update([
    'password'=> Hash::make($request->new_password)
    ]);
    return Redirect::back();

    }
    }

    Create routes

    Now you need to add below two routes for showing the view and posting password change request.
    1
    2
    Route::get('change-password', '[email protected]');
    Route::post('change-password', '[email protected]')->name('change.password');

    Setting up blade file

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    @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 your password') }}</div>

    <div class="card-body">
    <form method="POST" action="{{ route('change.password') }}">

    @csrf

    <div class="form-group row">
    <label for="password" class="col-md-4 col-form-label text-md-right">Current Password</label>

    <div class="col-md-6">
    <input id="password" type="password" class="form-control" name="current_password" autocomplete="current-password">
    </div>

    </div>

    <div class="form-group row">
    <label for="password" class="col-md-4 col-form-label text-md-right">New Password</label>

    <div class="col-md-6">
    <input id="new_password" type="password" class="form-control" name="new_password" autocomplete="current-password">
    </div>
    </div>

    <div class="form-group row">
    <label for="password" class="col-md-4 col-form-label text-md-right">New Confirm Password</label>

    <div class="col-md-6">
    <input id="new_confirm_password" type="password" class="form-control" name="new_confirm_password" autocomplete="current-password">
    </div>
    </div>

    @foreach ($errors->all() as $error)
    <p class="text-danger">{{ $error }}</p>
    @endforeach

    <div class="form-group row mb-0">
    <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

    Run our application

    Now we have already finished our application. Now we can run your application using below command:
    1
    php artisan serve

    Now, navigate to http://localhost:8000 in your browser.

    Conclusion

    In this tutorial, we implemented password change functionality in Laravel. If you have any issue regarding this tutorial, mention your issue in comment section or reach me through my E-mail. You can obtain complete source code and asset files for this tutorial from this GitHub repository.