Change password in laravel

Created at 14-May-2020 , By samar

Html form to get the user input data password and new password.

<form id="form-change-password" role="form" method="POST" action="{{ url('/user/credentials') }}" novalidate class="form-horizontal">
    <div class="col-md-9">
        <label for="current-password" class="col-sm-4 control-label">Current Password</label>
        <div class="col-sm-8">
            <div class="form-group">
                <input type="hidden" name="_token" value="{{ csrf_token() }}">
                <input type="password" class="form-control" id="current-password" name="current-password" placeholder="Password">
            </div>
        </div>
        <label for="password" class="col-sm-4 control-label">New Password</label>
        <div class="col-sm-8">
            <div class="form-group">
                <input type="password" class="form-control" id="password" name="password" placeholder="Password">
            </div>
        </div>
        <label for="password_confirmation" class="col-sm-4 control-label">Re-enter Password</label>
        <div class="col-sm-8">
            <div class="form-group">
                <input type="password" class="form-control" id="password_confirmation" name="password_confirmation" placeholder="Re-enter Password">
            </div>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-5 col-sm-6">
            <button type="submit" class="btn btn-danger">Submit</button>
        </div>
    </div>
</form>

function to validate request form data.

public function credential_rules(array $data)
{
    $messages = [
        'current-password.required' => 'Please enter current password',
        'password.required' => 'Please enter password',
    ];

    $validator = Validator::make($data, [
        'current-password' => 'required',
        'password' => 'required|same:password',
        'password_confirmation' => 'required|same:password',
    ], $messages);

    return $validator;
}

Function to change the password.

public function postCredentials(Request $request)
{
    if(Auth::Check())
    {
        $request_data = $request->All();
        $validator = $this->credential_rules($request_data);
        if($validator->fails())
        {
            return response()->json(array('error' => $validator->getMessageBag()->toArray()), 400);
        }
        else
        {
            $current_password = Auth::User()->password;
            if(Hash::check($request_data['current-password'], $current_password))
            {
                $user_id = Auth::User()->id;
                $obj_user = User::find($user_id);
                $obj_user->password = Hash::make($request_data['password']);
                $obj_user->save();
                return "ok";
            }
            else
            {
                $error = array('current-password' => 'Please enter correct current password');
                return response()->json(array('error' => $error), 400);
            }
        }
    }
    else
    {
        return redirect()->to('/');
    }
}

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.