dimanche 18 octobre 2020

Laravel API controller | How can i hash the password in this "update" method of the controller?

This is the method inside my API controller which is suppose to update the User data. One of the columns is password which is suppose to be hashed before insert.

public function update($id, Request $request)
{
    $user = $this->userRepository->findWithoutFail($id);
    
    if (empty($user)) {
        return $this->sendResponse([
            'error' => true,
            'code' => 404,
        ], 'User not found');
    }
    $input = $request->except(['api_token']);
    try {
        if ($request->has('device_token')) {
            $user = $this->userRepository->update($request->only('device_token'), $id);
        } else {
            $customFields = $this->customFieldRepository->findByField('custom_field_model', $this->userRepository->model());
           
            $user = $this->userRepository->update($input, $id);
            
            foreach (getCustomFieldsValues($customFields, $request) as $value) {
            
                $user->customFieldsValues()
                    ->updateOrCreate(['custom_field_id' => $value['custom_field_id']], $value);
            }
        }
    } catch (ValidatorException $e) {
        return $this->sendError($e->getMessage(), 401);
    }

    return $this->sendResponse($user, __('lang.updated_successfully', ['operator' => __('lang.user')]));
}

Right now, upon updating the User data using this method, the raw text of the password is inserted to the User table. I want it to be hashed using the following code. But i do not know where should this go in the method above. Can someone please help ??

 $user->password = Hash::make($request->input('password'));

Example of the body of request which would come to update :

{"id":"147","name":"tipumon","email":"a@gmail.com","phone":"9898365627","useraddress":"test address","password":"passwordtext","api_token":"<apiTokenstring>","bio":"","media":{"id":null,"name":null,"url":"https://example.com/public/images/image_default.png","thumb":"https://example.com/public/images/image_default.png","icon":"https://example.com/public/images/image_default.png","formated_size":null}}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire