php - How to properly update password in Laravel?

526

I have a Laravel application where I use a custom user authentication. Now i'm trying to allow user to update their password, the issue I'm running into there is that I need to check for the old password, if the field is not empty than I want to check if the new password matches the password repeat filed, if it does than update it here is the code I have in controller:

$newpass = $request->input('password');
$passrepeat = $request->input('passwordRepeat');

if ($oldPass = $request->input('oldPassword') != null)
{
  if (Hash::check($request->oldPassword, auth()->user()->password)){
       if ($newpass = $passrepeat){

                 $password = bcrypt($request['password']);
                 $user->password = $password;

       }
  }
}

and then I update it:

$user->update();

But my code seem to not even check for the password field. I'm clearly making mistakes when I check for the oldPassword field. There are just too many options that I can try so I decided ask as a question.

Laravel 5.4

370

Answer

Solution:

From the docs:

Thecheck method allows you to verify that a given plain-text string corresponds to a given hash. However, if you are using theLoginController included with Laravel, you will probably not need to use this directly, as this controller automatically calls this method:

if (Hash::check('plain-text', $hashedPassword)) {

https://laravel.com/docs/5.5/hashing#basic-usage

380

Answer

Solution:

Just Replace these two lines

  $password = bcrypt($request['password']);
  $user->password = $password;

With

 $user->password = password_hash($request->password, PASSWORD_DEFAULT);

People are also looking for solutions to the problem: php - how to run xml request on wsdl url

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.