feat: override save method in Profile page to handle custom password session updates and transaction management

This commit is contained in:
Yoga Pangestu 2026-03-31 14:42:55 +07:00
parent a072580761
commit fcda1c3914

View File

@ -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();