diff --git a/app/Components/Profile/Manager.php b/app/Components/Profile/Manager.php index 1263752..b0920a5 100644 --- a/app/Components/Profile/Manager.php +++ b/app/Components/Profile/Manager.php @@ -4,13 +4,26 @@ namespace App\Components\Profile; use Livewire\Component; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Hash; use Spatie\Permission\Models\Role; class Manager extends Component { - public object $user; - public array $roles = []; - public array $allRoles = []; + // Profile & roles + public object $user; + public array $roles = []; + public array $allRoles = []; + + // Đổi mật khẩu + public string $mode = 'view'; // 'view' | 'password' + public string $currentPassword = ''; + public string $password = ''; + public string $password_confirmation = ''; + + protected array $rules = [ + 'currentPassword' => ['required', 'string'], + 'password' => ['required', 'string', 'min:8', 'confirmed'], + ]; public function mount(): void { @@ -19,6 +32,40 @@ class Manager extends Component $this->roles = $this->user->roles->pluck('name')->toArray(); } + public function showPasswordForm(): void + { + $this->mode = 'password'; + $this->resetErrorBag(); + $this->resetValidation(); + } + + public function showView(): void + { + $this->mode = 'view'; + $this->reset(['currentPassword', 'password', 'password_confirmation']); + $this->resetErrorBag(); + } + + public function updatePassword(): void + { + // 1. Validate + $this->validate(); + + // 2. Kiểm tra mật khẩu hiện tại + if (! Hash::check($this->currentPassword, $this->user->password)) { + $this->addError('currentPassword', 'Mật khẩu hiện tại không đúng.'); + return; + } + + // 3. Cập nhật mật khẩu mới + $this->user->password = Hash::make($this->password); + $this->user->save(); + + // 4. Thông báo & quay về view + session()->flash('message', 'Đổi mật khẩu thành công.'); + $this->showView(); + } + public function render() { return view('components.profile.manager', [ diff --git a/resources/views/components/profile/manager.blade.php b/resources/views/components/profile/manager.blade.php index b70945b..7ff0d32 100644 --- a/resources/views/components/profile/manager.blade.php +++ b/resources/views/components/profile/manager.blade.php @@ -1,13 +1,22 @@ -