1

I'm using the authentication included with laravel 5.2:

https://laravel.com/docs/5.2/authentication#included-routing

I want to implement a 'change password' functionality for authenticated users and I have two options:

  1. I can use the included '/password/reset' route, not optimal but ok. But it only works when the user is not logged in. Can I make it work for authenticated users as well?

  2. I can create an 'enter new password' form in a view and a updatePassword method in UsersController. But I need to check if the submitted password is not empty, that means the user wants to change it, then apply validation rules, then encrypt it and update it. Is it the right way of doing it? Any examples will be appreciated.

asked Feb 17, 2016 at 20:29

3 Answers 3

1

I think #2 is the way to go, it's just another field in the user settings form. In my app it looks like this:

$user->update($request->except('password'));
if($request->get('password') != ""){
 $user->password = Hash::make($request->get('password'));
}
$user->save();

I do a special check to ensure I don't update the password from a blank field. Add validation to taste.

answered Feb 17, 2016 at 20:53
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I'm going for that option. Thanks.
1

You can set set attribute in model like

 public function setPasswordAttribute($value)
 {
 $this->attributes['password'] = bcrypt($value);
}

In edit method:

 if (empty($request->password)) {
 $data = $request->except('password');
 } else {
 $data = $request->all();
 }
 $user = $this->userRepository->update($data, $id);
answered May 21, 2016 at 5:47

Comments

1

I have implemented change password in my user file as below

public function postReset(Request $request)
 { 
 $this->validate($request, [
 'password' => 'required|confirmed','email' => 'required|email',
 ]);
 $user = User::findOrFail($id);
 $input = $request->input();
 //Change Password if password value is set
 if ($input['password'] != "") {
 //dd(bcrypt($input['password']));
 $input['password'] = bcrypt($input['password']);
 }
 $user->fill($input)->save();
 }
answered Aug 3, 2016 at 12:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.