I’m new to Laravel. I have created custom Change Password in Laravel 8 using Livewire. But, after succeeded in updating the user password, my session is expired and redirected to login page. So, the question is how to keep the session alive and redirect to the current page?
Here’s my code:
ChangeUserPassword.php
class ChangeUserPassword extends Component { public $oldPassword; public $newPassword; public $confirmPassword; public function render() { return view('livewire.auth.change-user-password'); } public function changePassword() { $this->validate([ 'oldPassword' => 'required', 'newPassword' => ['required', Password::min(8) ->letters() ->mixedCase() ->numbers() ->symbols() // ->uncompromised() ], 'confirmPassword' => 'required|min:8|same:newPassword' ]); $user = User::find(auth()->user()->id); if (Hash::check($this->oldPassword, $user->password)) { $user->update([ 'password' => Hash::make($this->newPassword), 'updated_at' => Carbon::now()->toDateTimeString() ]); $this->emit('showAlert', [ 'msg' => 'Your password has been successfully changed.' ]); return redirect()->route('user.changepassword'); } else { $this->emit('showAlertError', [ 'msg' => 'Old password does not match.' ]); } } }
change-user-password.blade.php
<div class="col-md-12"> <div class="card"> <div class="card-body"> <h4 class="card-title ml-2">Change Password</h4> <form wire:submit.prevent="changePassword" role="form"> @csrf <div class="row"> <div class="form-group col-md-4"> <label for="oldPassword" class="form-label">Old Password<span style="color: red"> *</span></label> <input class="form-control @error('oldPassword') is-invalid @enderror" wire:model="oldPassword" name="oldPassword" id="oldPassword" type="password" /> @error('oldPassword') <small id="helpId" class="text-danger">{{ $message }}</small> @enderror </div> <div class="form-group col-md-4"> <label for="newPassword" class="form-label">New Password<span style="color: red"> *</span></label> <input class="form-control @error('newPassword') is-invalid @enderror" wire:model="newPassword" name="newPassword" id="newPassword" type="password" /> @error('newPassword') <small id="helpId" class="text-danger">{{ $message }}</small> @enderror </div> <div class="form-group col-md-4"> <label for="confirmPassword" class="form-label">Confirm Password<span style="color: red"> *</span></label> <input class="form-control @error('confirmPassword') is-invalid @enderror" wire:model="confirmPassword" name="confirmPassword" id="confirmPassword" type="password" /> @error('confirmPassword') <small id="helpId" class="text-danger">{{ $message }}</small> @enderror </div> <div class="form-group"> <button type="submit" class="btn btn-primary pull-right" wire:loading.attr="disabled">Save</button> {{-- <div wire:loading> <img style="width: 25px;" src="{{ asset('assets/images/spinner-small.gif') }}" alt="Loading"> </div> --}} </div> </div> </form> </div> </div> </div> <script> document.addEventListener('livewire:load', function (e) { e.preventDefault() }) </script>
Any suggestion would really help. Thanks.
Advertisement
Answer
Authenticate the user again after updating the password
if (Hash::check($this->oldPassword, $user->password)) { $user->update([ 'password' => Hash::make($this->newPassword), 'updated_at' => Carbon::now()->toDateTimeString() ]); $this->emit('showAlert', [ 'msg' => 'Your password has been successfully changed.' ]); if(Auth::attempt(['email'=>$user->email, 'password'=>$this->newPassword])){ $request->session()->regenerate(); return redirect()->intended('user.changepassword'); } } else { $this->emit('showAlertError', [ 'msg' => 'Old password does not match.' ]); }