feat: add CustomProfile page for user profile editing with validation and password management
This commit is contained in:
parent
c0ae70f89e
commit
efe52fe909
112
app/Filament/Pages/Profile.php
Normal file
112
app/Filament/Pages/Profile.php
Normal file
@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Pages;
|
||||
|
||||
use Filament\Auth\Pages\EditProfile;
|
||||
use Filament\Facades\Filament;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Schemas\Components\Utilities\Get;
|
||||
use Filament\Schemas\Schema;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Validation\Rules\Password;
|
||||
|
||||
class Profile extends EditProfile
|
||||
{
|
||||
public function form(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
Section::make('Akun')
|
||||
->schema([
|
||||
TextInput::make('name')
|
||||
->label('Nama')
|
||||
->placeholder('John Doe')
|
||||
->autocomplete(false)
|
||||
->autofocus()
|
||||
->required()
|
||||
->maxLength(100),
|
||||
|
||||
TextInput::make('email')
|
||||
->label('Alamat Surel')
|
||||
->placeholder('johndoe@puskesmas.com')
|
||||
->autocomplete(false)
|
||||
->required()
|
||||
->maxLength(254)
|
||||
->unique(ignoreRecord: true)
|
||||
->email()
|
||||
->live(),
|
||||
|
||||
TextInput::make('username')
|
||||
->label('Nama Pengguna')
|
||||
->placeholder('johndoe')
|
||||
->autocomplete(false)
|
||||
->required()
|
||||
->minLength(5)
|
||||
->maxLength(20)
|
||||
->regex('/^[a-zA-Z0-9_.-]+$/')
|
||||
->unique(ignoreRecord: true),
|
||||
]),
|
||||
|
||||
Section::make('Kata Sandi')
|
||||
->schema([
|
||||
TextInput::make('new_password')
|
||||
->label(__('filament-panels::auth/pages/edit-profile.form.password.label'))
|
||||
->validationAttribute(__('filament-panels::auth/pages/edit-profile.form.password.validation_attribute'))
|
||||
->password()
|
||||
->revealable(filament()->arePasswordsRevealable())
|
||||
->rule(Password::default())
|
||||
->showAllValidationMessages()
|
||||
->autocomplete('new-password')
|
||||
->dehydrated(fn ($state): bool => filled($state))
|
||||
->dehydrateStateUsing(fn ($state): string => Hash::make($state))
|
||||
->live(debounce: 500)
|
||||
->same('passwordConfirmation')
|
||||
->placeholder('********'),
|
||||
|
||||
TextInput::make('passwordConfirmation')
|
||||
->label(__('filament-panels::auth/pages/edit-profile.form.password_confirmation.label'))
|
||||
->validationAttribute(__('filament-panels::auth/pages/edit-profile.form.password_confirmation.validation_attribute'))
|
||||
->password()
|
||||
->autocomplete('new-password')
|
||||
->revealable(filament()->arePasswordsRevealable())
|
||||
->required()
|
||||
->visible(fn (Get $get): bool => filled($get('new_password')))
|
||||
->dehydrated(false)
|
||||
->placeholder('********'),
|
||||
|
||||
TextInput::make('currentPassword')
|
||||
->label(__('filament-panels::auth/pages/edit-profile.form.current_password.label'))
|
||||
->validationAttribute(__('filament-panels::auth/pages/edit-profile.form.current_password.validation_attribute'))
|
||||
->belowContent(__('filament-panels::auth/pages/edit-profile.form.current_password.below_content'))
|
||||
->password()
|
||||
->autocomplete('current-password')
|
||||
->currentPassword(guard: Filament::getAuthGuard())
|
||||
->revealable(filament()->arePasswordsRevealable())
|
||||
->required()
|
||||
->visible(fn (Get $get): bool => filled($get('new_password')) || ($get('email') !== $this->getUser()->getAttributeValue('email')))
|
||||
->dehydrated(false)
|
||||
->placeholder('********'),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
protected function handleRecordUpdate(Model $record, array $data): Model
|
||||
{
|
||||
$shouldLogout = false;
|
||||
if (isset($data['new_password'])) {
|
||||
$data['password'] = $data['new_password'];
|
||||
unset($data['new_password']);
|
||||
$shouldLogout = true;
|
||||
}
|
||||
|
||||
$record->update($data);
|
||||
|
||||
if ($shouldLogout) {
|
||||
auth()->logout();
|
||||
}
|
||||
|
||||
return $record;
|
||||
}
|
||||
}
|
||||
@ -3,6 +3,7 @@
|
||||
namespace App\Providers\Filament;
|
||||
|
||||
use App\Filament\Pages\Auth\Login;
|
||||
use App\Filament\Pages\Profile;
|
||||
use BezhanSalleh\FilamentShield\FilamentShieldPlugin;
|
||||
use DiogoGPinto\AuthUIEnhancer\AuthUIEnhancerPlugin;
|
||||
use Filament\Http\Middleware\Authenticate;
|
||||
@ -31,6 +32,7 @@ public function panel(Panel $panel): Panel
|
||||
->path('admin')
|
||||
->viteTheme('resources/css/filament/admin/theme.css')
|
||||
->login(Login::class)
|
||||
->profile(Profile::class)
|
||||
->colors([
|
||||
'primary' => Color::Violet,
|
||||
])
|
||||
|
||||
Loading…
Reference in New Issue
Block a user