What worked for me in a Laravel 8 project, as derived from the answers of manniL (use of Auth::login
) and Lukmon Awoyemi (use the remember me functionality):
public function updatePassword(UpdatePasswordRequest $request) {
$user = $request->user();
$user->fill([
'password' => Hash::make($request->password),
])->save();
// make sure to re-login the user
Auth::login($user, !!$user->getRememberToken());
$request->session()->flash('status', 'Password updated!');
return redirect()->route('some.route');
}
The UpdatePasswordRequest
is just a form request which validates the given original and new passwords, checking that the original password provided is valid and the new password is confirmed.
The addition here is to first check for a remember token. This check will return a token or null
if no remember token was set. This information will be converted to boolean and provided to the login
function.
Logging in the user like this will make sure that all necessary password and login hashes are updated in the session to match the new password hash. Therefore, the AuthenticateSession
middleware will still recognize the user as being logged in. Also, the user is remembered if and only if s/he was remembered before the password change.