lasthourcloud/app/Livewire/Profile/Index.php

82 lines
2.1 KiB
PHP
Raw Normal View History

2023-05-22 20:30:33 +00:00
<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Profile;
2023-05-22 20:30:33 +00:00
2024-03-13 09:10:53 +00:00
use Illuminate\Support\Facades\Hash;
2023-12-08 08:15:32 +00:00
use Livewire\Attributes\Validate;
2023-05-22 20:30:33 +00:00
use Livewire\Component;
2024-01-07 15:23:41 +00:00
class Index extends Component
2023-05-22 20:30:33 +00:00
{
public int $userId;
2024-06-10 20:43:34 +00:00
2023-05-22 20:30:33 +00:00
public string $email;
2024-03-13 09:10:53 +00:00
public string $current_password;
2024-06-10 20:43:34 +00:00
2024-03-13 09:10:53 +00:00
public string $new_password;
2024-06-10 20:43:34 +00:00
2024-03-13 09:10:53 +00:00
public string $new_password_confirmation;
2023-12-08 08:15:32 +00:00
#[Validate('required')]
public string $name;
2024-06-10 20:43:34 +00:00
2023-05-22 20:30:33 +00:00
public function mount()
{
$this->userId = auth()->user()->id;
$this->name = auth()->user()->name;
$this->email = auth()->user()->email;
}
2024-06-10 20:43:34 +00:00
2023-05-22 20:30:33 +00:00
public function submit()
{
try {
$this->validate([
'name' => 'required',
]);
2023-12-08 08:15:32 +00:00
auth()->user()->update([
2023-05-22 20:30:33 +00:00
'name' => $this->name,
]);
2023-12-08 08:15:32 +00:00
2024-03-13 09:10:53 +00:00
$this->dispatch('success', 'Profile updated.');
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
2024-06-10 20:43:34 +00:00
2024-03-13 09:10:53 +00:00
public function resetPassword()
{
try {
$this->validate([
'current_password' => 'required',
'new_password' => 'required|min:8',
'new_password_confirmation' => 'required|min:8|same:new_password',
]);
2024-06-10 20:43:34 +00:00
if (! Hash::check($this->current_password, auth()->user()->password)) {
2024-03-13 09:10:53 +00:00
$this->dispatch('error', 'Current password is incorrect.');
2024-06-10 20:43:34 +00:00
2024-03-13 09:10:53 +00:00
return;
}
if ($this->new_password !== $this->new_password_confirmation) {
$this->dispatch('error', 'The two new passwords does not match.');
2024-06-10 20:43:34 +00:00
2024-03-13 09:10:53 +00:00
return;
}
auth()->user()->update([
'password' => Hash::make($this->new_password),
]);
$this->dispatch('success', 'Password updated.');
$this->current_password = '';
$this->new_password = '';
$this->new_password_confirmation = '';
2023-06-09 13:55:21 +00:00
} catch (\Throwable $e) {
return handleError($e, $this);
2023-05-22 20:30:33 +00:00
}
}
2024-06-10 20:43:34 +00:00
2024-01-07 15:23:41 +00:00
public function render()
{
return view('livewire.profile.index');
}
2023-05-22 20:30:33 +00:00
}