在 Laravel 8 中存储数据后如何保持会话?
How to keep session after storing data in Laravel 8?
我是 Laravel 的新手。我使用 Livewire 在 Laravel 8 中创建了自定义更改密码。但是,在成功更新用户密码后,我的会话已过期并重定向到登录页面。那么,问题是如何保持会话存活并重定向到当前页面?
这是我的代码:
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.'
]);
}
}
}
更改用户-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>
任何建议都会很有帮助。谢谢
更新密码后再次验证用户
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.'
]);
}
我是 Laravel 的新手。我使用 Livewire 在 Laravel 8 中创建了自定义更改密码。但是,在成功更新用户密码后,我的会话已过期并重定向到登录页面。那么,问题是如何保持会话存活并重定向到当前页面?
这是我的代码:
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.'
]);
}
}
}
更改用户-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>
任何建议都会很有帮助。谢谢
更新密码后再次验证用户
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.'
]);
}