Wednesday, 19 July 2023

Laravel Change password

Controller
<?php
 private $currentPass;
 private $newPass;

public function changePassword(Request $request)
{
    try {
        if ($request->isMethod('post')) {
            $this->currentPass = $request->currentPassword;
            $this->newPass = $request->newPassword;

            $validator = Validator::make(
                $request->only('currentPassword', 'newPassword', 'password_confirmation'),
                [
                    'currentPassword' => ['required'],
                    'newPassword' => ['required', Password::min(8)->numbers()->letters()->symbols()],
                    'password_confirmation' => 'required|same:newPassword'
                ]
            );
            $validator->after(function ($validator) {
                if (!Hash::check($this->currentPass, Auth::user()->password)) {
                    $validator->errors()->add(
                        'currentPassword',
                        'Current password is invalid'
                    );
                }
            });
            if ($validator->fails()) {
                return redirect()->back()->withErrors($validator)->withInput();
            }
            $user = User::find(Auth::user()->id);
            $user->password = Hash::make($this->newPass);
            if ($user->save()) {
                if (Auth::attempt(['email' => $user->email, 'password' => $this->newPass])) {
                    $request->session()->regenerate();
                }
                return redirect()->back()->with('success', 'Password has been updated successfully');
            } else {
                return redirect()->back()->with('error', 'password could not be updated successfully');
            }
        }
        return view('front.change_password');
    } catch (Exception $e) {
        abort(500);
    }
}
-------------------------------------

-----------------------------------------------------------------------
Blade

@extends('layouts.front')
@section('content')
    <section class="content-header">
        <h1>
            Change Password
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-cog"></i> Settings</a></li>
            <li class="active">Change Password</li>
            {{-- <li class="active">General Elements</li> --}}
        </ol>
    </section>
    <section class="content">
        @if (Session::has('success'))
            <div class="alert alert-success alert-dismissible">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                <h5><i class="icon fa fa-check"></i> {{ Session::get('success') }}</h5>
            </div>
        @endif
        <div class="row " style="align-items:center">
            <!-- right column -->
            <div class="col-md-12">
                <!-- Horizontal Form -->
                <div class="box box-info">
                    <div class="box-header with-border">
                        <h3 class="box-title">Change Password</h3>
                    </div>
                    <!-- /.box-header -->
                    <!-- form start -->
                    <form class="form-horizontal" action="{{ route('changePasswordUpdate') }}" method="POST">
                        @csrf
                        <div class="box-body">
                            <div class="row">
                                <div class="form-group">
                                    <div class="col-sm-3 text-right {{isset($errors) && $errors->has('currentPassword') ? 'has-error' : ''}} {{ Helper::HasError($errors, 'currentPassword') }}">
                                        <label class="form-label">Current Password <span style="color: red">*</span></label>
                                    </div>
                                    <div class="col-sm-8 {{ Helper::HasError($errors, 'currentPassword') }}">

                                        <input id="currentPassword" name="currentPassword" type="text"
                                            class="form-control" />
                                        <span class="help-block">
                                            <strong>{{ $errors->first('currentPassword') }}</strong>
                                        </span>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="form-group">
                                    <div class="col-sm-3 text-right {{ Helper::HasError($errors, 'newPassword') }}">
                                        <label class="form-label">New Password <span style="color: red">*</span></label>
                                    </div>
                                    <div class="col-sm-8 {{ Helper::HasError($errors, 'newPassword') }}">
                                        <div class="input-group">
                                            <input id="newPassword" name="newPassword" type="password"
                                                class="form-control" />
                                            <span class="input-group-addon show-password" style="cursor: pointer"><i
                                                    class="fa fa-eye"></i></span>
                                        </div>
                                        <span class="help-block">
                                            <strong>{{ $errors->first('newPassword') }}</strong>
                                        </span>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="form-group">
                                    <div class="col-sm-3 text-right {{ Helper::HasError($errors, 'password_confirmation') }}">
                                        <label class="form-label">Confirm Password <span style="color: red">*</span></label>
                                    </div>
                                    <div class="col-sm-8 {{ Helper::HasError($errors, 'password_confirmation') }}">
                                        <div class="input-group">
                                            <input id="password_confirmation" name="password_confirmation" type="password"
                                                class="form-control" />
                                            <span class="input-group-addon show-con-password" style="cursor: pointer"><i
                                                    class="fa fa-eye"></i></span>
                                        </div>
                                        <span class="help-block">
                                            <strong>{{ $errors->first('password_confirmation') }}</strong>
                                        </span>
                                        <span for="" class="text-warning"><i>Password: minimum 8, contain
                                                letters,symbols,numbers</i></span>
                                    </div>
                                </div>
                            </div>

                        </div>
                        <!-- /.box-body -->
                        <div class="box-footer">
                            {{-- <button type="submit" class="btn btn-default">Cancel</button> --}}
                            <button type="submit" class="btn btn-primary pull-right">Submit</button>
                        </div>
                        <!-- /.box-footer -->
                    </form>
                </div>
                <!-- /.box -->
            </div>
            <!--/.col (right) -->
        </div>
        <!-- /.row -->
    </section>
@endsection
@push('scripts')
    <script>
        document.querySelector('.show-password').addEventListener('click', showPassword);

        function showPassword() {
            const passType = document.querySelector('#newPassword').getAttribute('type');
            if (passType == 'password') {
                document.querySelector('#newPassword').setAttribute('type', 'text');
            }
            if (passType == 'text') {
                document.querySelector('#newPassword').setAttribute('type', 'password');
            }
        }

        document.querySelector('.show-con-password').addEventListener('click', showConPassword);

        function showConPassword() {
            const passType = document.querySelector('#password_confirmation').getAttribute('type');
            if (passType == 'password') {
                document.querySelector('#password_confirmation').setAttribute('type', 'text');
            }
            if (passType == 'text') {
                document.querySelector('#password_confirmation').setAttribute('type', 'password');
            }
        }
    </script>
@endpush

------------------------------------------
Routes

Route::get('/change-password', [HomeController::class,'changePassword'])->name('changePasswordEdit');
    Route::post('/change-password', [HomeController::class,'changePassword'])->name('changePasswordUpdate');

No comments:

Post a Comment

Laravel Export data to csv

 use Illuminate\Http\Response; // Define a function to export data to CSV function exportToCSV($exportData, $columns) {     $filename = ...