Laravel 5.3 - How to change the password of a logged in user?

1

I'm having trouble creating a method that will allow me to provide a password change form for logged-in users. I would like to use the methods of the class: App \ Http \ Controllers \ Auth \ ResetPasswordController.

Currently I have a view with 3 fields, they are:

  • current_password
  • new_password
  • nova_senha_confirmation

In the controller I have a method with the code to validate the data received and persist in the database:

    $this->validate($request, [
                                'senha_atual' => 'required|min:8',
                                'nova_senha' => 'required|min:8|confirmed']);

    if (!Hash::check($request->senha_atual, Auth::user()->password))
    {
        return redirect('/painel/alterarSenha')->withErrors(['senha_atual' => 'Senha incorreta'])->withInput();;
    }

    $resultado = $request->user()->fill([
        'password' => Hash::make($request->input('nova_senha'))
    ])->save();

    if($resultado)
    {
        return redirect('/painel')->with(['success' => 'Senha alterada com sucesso!']);
    }

But as I already mentioned, I would like to use the methods of controlling ResetPasswordController that extends from Illuminate \ Foundation \ Auth \ ResetsPasswords, even to follow a solid approach and without duplication of code.

    
asked by anonymous 27.10.2016 / 16:22

2 answers

1

Using Hash :: check as you used it, it worked for me.

 public function updateOwn(Request $request, $id)
    {

        $input = $request->all();

        if (! Hash::check($input['password_old'],Auth::user()->password)){
            return redirect('users/edit_own')->withErrors(['password' => 'Senha atual está incorreta'])->withInput();
        }

        $validator = Validator::make($request->all(), [
            'email'      => 'required|email|unique:users,email,' . $input['id'],
            'password'   => ["required"],
            'password_confirmation' => 'required|same:password'
        ]);

        if ($validator->fails()) {
            return redirect('users/edit_own')
                ->withErrors($validator)
                ->withInput();
        }
        $user = $this->users->find($id);

        $input['password'] = bcrypt($input['password']);//criptografa password

        $user->update($input);
        flash()->success('Usuário atualizado com sucesso. - '.$user->name);//flash message teste
        return redirect()->route('/home');
    }

The user can only change their data if they confirm the previous password.

    
05.04.2017 / 01:18
0

But have you tried using this method yet? Normally I do exactly the way you did above and only use the ResetPassword method for users who have lost the password.

I think the way you want to use it will not work because the Resetpassword method has a middleware ('guest') that will cause the user to be redirected if logged in.

Now you've discovered a better way to do it, because I'm new to the area and would like to learn about other methods.

I hope you've been helpful, hug.

    
02.11.2016 / 19:16