From fcda1c391489ef2cdb4126c12bcee964564cf849 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Tue, 31 Mar 2026 14:42:55 +0700 Subject: [PATCH] feat: override save method in Profile page to handle custom password session updates and transaction management --- app/Filament/Pages/Profile.php | 51 ++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/app/Filament/Pages/Profile.php b/app/Filament/Pages/Profile.php index b4f6eac..f82aff7 100644 --- a/app/Filament/Pages/Profile.php +++ b/app/Filament/Pages/Profile.php @@ -18,9 +18,12 @@ use Filament\Schemas\Components\Section; use Filament\Schemas\Schema; use Filament\Support\Enums\Width; +use Filament\Support\Exceptions\Halt; +use Filament\Support\Facades\FilamentView; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Hash; use Illuminate\Validation\Rules\Password; +use Throwable; class Profile extends EditProfile { @@ -232,6 +235,54 @@ public function form(Schema $schema): Schema ]); } + public function save(): void + { + try { + $this->beginDatabaseTransaction(); + + $this->callHook('beforeValidate'); + + $data = $this->form->getState(); + + $this->callHook('afterValidate'); + + $data = $this->mutateFormDataBeforeSave($data); + + $this->callHook('beforeSave'); + + $this->handleRecordUpdate($this->getUser(), $data); + + $this->callHook('afterSave'); + } catch (Halt $exception) { + $exception->shouldRollbackDatabaseTransaction() ? + $this->rollBackDatabaseTransaction() : + $this->commitDatabaseTransaction(); + + return; + } catch (Throwable $exception) { + $this->rollBackDatabaseTransaction(); + + throw $exception; + } + + $this->commitDatabaseTransaction(); + + if (request()->hasSession() && array_key_exists('password', $data)) { + request()->session()->put([ + 'password_hash_'.filament()->getAuthGuard() => $data['password'], + ]); + } + + $this->data['password'] = null; + $this->data['passwordConfirmation'] = null; + + $this->getSavedNotification()?->send(); + + if ($redirectUrl = $this->getRedirectUrl()) { + $this->redirect($redirectUrl, navigate: FilamentView::hasSpaMode($redirectUrl)); + } + } + protected function mutateFormDataBeforeFill(array $data): array { $user = $this->getUser();