Laravel

How To Change Password In Laravel 

How To Change Password In Laravel 

I will explain how to change a password in Laravel.

For Change Password In Laravel first we have set up Laravel project with its authentication system, I will walk you through the steps to change the password for a logged-in user.

  1. Create Change Password Route in web.php
  2. Create View file for change password
  3. Write function in Controller
  4. Handle form submit and show success message

Step 1: Create Change Password Route in web.php

Create route for change password

Route::any('/user/change-password', [App\Http\Controllers\CustomerAuthController::class, 'changePassword'])->name('/user/change-password');

Step 2: Create View file for change password

Create a change-password.blade.php file in the views folder to design the change password interface.

<div class="col-md-12">
                    <form action="" method="post" action="">
                        {{ csrf_field() }}

                        @if (\Session::has('success'))
                            <div class="col-xs-12">
                                <li class="alert alert-success">{!! \Session::get('success') !!}</li>
                            </div>
                        @endif
                        @if (\Session::has('error'))
                            <div class="col-xs-12">
                                <li class="alert alert-danger">{!! \Session::get('error') !!}</li>
                            </div>
                        @endif

                        <div class="form-group has-feedback">
                            @if ($errors->any())
                                <div class="error" style="color: red">
                                    {!! implode('', $errors->all('<div>:message</div>')) !!}
                                </div>
                            @endif
                        </div>
                         @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="row">
                           
                            <div class="col-md-6 mb-3 p-3">
                                

                                    
                       
                                     <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" required>
                                @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" required>
                                @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" required>
                            </div>
                                    <div class="col-md-12">
                                        <div class="form-group">
                                            <button type="submit" class="btn btn-primary">Update</button>
                                        </div>
                                    </div>
                               
                            </div>

                        </div>
                    </form>
                </div>

The output look like bellow

Step 3: Write function in Controller

Create changePassword function in app/Http/Controllers/CustomerAuthController

    public function changePassword(Request $request)
{
    if ($request->method() == 'GET') {
            return view('change-password');
        }
        
            if ($request->method() == 'POST') {

        # Validation
        $request->validate([
            'old_password' => 'required',
            'new_password' => 'required|confirmed',
        ]);


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


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

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

If you encounter any issues during implementation, please leave a comment with your query.

Thank you for reading.

Visited 14 times, 1 visit(s) today

Leave a Reply

Your email address will not be published. Required fields are marked *