How to change password in laravel 9
Tags: laravel, webdev, beginners, tutorial
Published: 2022-03-02 21:50:47

I am going to explain here, how you can change the password in laravel 9.

I have [installed the laravel](https://dev.to/shanisingh03/how-to-install-laravel-9-25c4) with [Laravel Authentication](https://dev.to/shanisingh03/how-to-make-login-registration-in-laravel-9-1p2n) and going to explain about changing password of logged in user.

## Step 1 - Change Password Page:

### Create a route for change password

```php
Route::get('/change-password', [App\Http\Controllers\HomeController::class, 'changePassword'])->name('change-password');
```

### Cerate `changePassword` function in `HomeController`
```php
public function changePassword()
{
   return view('change-password');
}
```

### Create `change-password.blade.php` view file in `resources/views`

```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">{{ __('Chnage Password') }}</div>

                    <form action="{{ route('update-password') }}" method="POST">
                        @csrf
                        <div class="card-body">
                            @if (session('status'))
                                <div class="alert alert-success" role="alert">
                                    {{ session('status') }}
                                </div>
                            @elseif (session('error'))
                                <div class="alert alert-danger" role="alert">
                                    {{ session('error') }}
                                </div>
                            @endif

                            <div class="mb-3">
                                <label for="oldPasswordInput" class="form-label">Old Password</label>
                                <input name="old_password" type="password" class="form-control @error('old_password') is-invalid @enderror" id="oldPasswordInput"
                                    placeholder="Old Password">
                                @error('old_password')
                                    <span class="text-danger">{{ $message }}</span>
                                @enderror
                            </div>
                            <div class="mb-3">
                                <label for="newPasswordInput" class="form-label">New Password</label>
                                <input name="new_password" type="password" class="form-control @error('new_password') is-invalid @enderror" id="newPasswordInput"
                                    placeholder="New Password">
                                @error('new_password')
                                    <span class="text-danger">{{ $message }}</span>
                                @enderror
                            </div>
                            <div class="mb-3">
                                <label for="confirmNewPasswordInput" class="form-label">Confirm New Password</label>
                                <input name="new_password_confirmation" type="password" class="form-control" id="confirmNewPasswordInput"
                                    placeholder="Confirm New Password">
                            </div>

                        </div>

                        <div class="card-footer">
                            <button class="btn btn-success">Submit</button>
                        </div>

                    </form>
                </div>
            </div>
        </div>
    </div>
@endsection

```
The Output Will look like.

![Change Password](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uajtxxmmr196e7o9aogk.png)


## Step 2 - Handle the Form Submit.

### Create a POST route in `routes/web.php` file
```php
Route::post('/change-password', [App\Http\Controllers\HomeController::class, 'updatePassword'])->name('update-password');
```

### Create `updatePassword` function in `app/Http/Controllers/HomeController`.

```php
public function updatePassword(Request $request)
{
        # Validation
        $request->validate([
            'old_password' => 'required',
            'new_password' => 'required|confirmed',
        ]);

        
        #Match The Old Password
        if(!Hash::check($request->old_password, auth()->user()->password)){
            return back()->with("error", "Old Password Doesn't match!");
        }
        

        #Update the new Password
        User::whereId(auth()->user()->id)->update([
            'password' => Hash::make($request->new_password)
        ]);

        return back()->with("status", "Password changed successfully!");
}
```

the final output will look like.

![Change Password](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t8wc63n0ezhivttywy4x.png)

The Complete Video Tutorial is below in the video.

{% youtube IM1bvPzJtTE %}

If you face any issue while implementing, please comment your query.

Thank You for Reading

Reach Out To me.
[Twitter](https://twitter.com/shanisingh03)
[Instagram](https://www.instagram.com/shanisingh03/)
[TechToolIndia YouTube Channel](https://www.youtube.com/channel/UCOy6o08Yn9DtXMKqxhD9ivA)