From 1dc187be9ec7770bba281af0bd9c2fd50bc404e6 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Tue, 10 Mar 2026 10:49:47 +0700 Subject: [PATCH] feat: Update admin panel login configuration to use custom Login page --- app/Filament/Pages/Auth/Login.php | 152 ++++++++++++++++++ app/Providers/Filament/AdminPanelProvider.php | 3 +- 2 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 app/Filament/Pages/Auth/Login.php diff --git a/app/Filament/Pages/Auth/Login.php b/app/Filament/Pages/Auth/Login.php new file mode 100644 index 0000000..687eb58 --- /dev/null +++ b/app/Filament/Pages/Auth/Login.php @@ -0,0 +1,152 @@ +components([ + TextInput::make('login') + ->label('Nama Pengguna atau Alamat Surel') + ->placeholder('johndoe@example.com') + ->required() + ->autocomplete(false) + ->autofocus(), + + TextInput::make('password') + ->label('Kata Sandi') + ->placeholder('********') + ->password() + ->revealable(filament()->arePasswordsRevealable()) + ->required() + ->autocomplete(false) + ->extraInputAttributes(['tabindex' => 2]) + ->hint( + filament()->hasPasswordReset() + ? new HtmlString(Blade::render('{{ __(\'filament-panels::auth/pages/login.actions.request_password_reset.label\') }}')) + : null + ), + ]); + } + + protected function getCredentialsFromFormData(#[SensitiveParameter] array $data): array + { + $loginType = filter_var($data['login'], FILTER_VALIDATE_EMAIL) ? 'email' : 'username'; + + return [ + $loginType => $data['login'], + 'password' => $data['password'], + ]; + } + + public function authenticate(): ?LoginResponse + { + try { + $this->rateLimit(5); + } catch (TooManyRequestsException $exception) { + $this->getRateLimitedNotification($exception)?->send(); + + return null; + } + + $data = $this->form->getState(); + + $authGuard = Filament::auth(); + + $authProvider = $authGuard->getProvider(); + /** @phpstan-ignore-line */ + $credentials = $this->getCredentialsFromFormData($data); + + $user = $authProvider->retrieveByCredentials($credentials); + + if ((! $user) || (! $authProvider->validateCredentials($user, $credentials))) { + $this->userUndertakingMultiFactorAuthentication = null; + + $this->fireFailedEvent($authGuard, $user, $credentials); + $this->throwFailureValidationException(); + } + + if ( + filled($this->userUndertakingMultiFactorAuthentication) && + (decrypt($this->userUndertakingMultiFactorAuthentication) === $user->getAuthIdentifier()) + ) { + $this->multiFactorChallengeForm->validate(); + } else { + foreach (Filament::getMultiFactorAuthenticationProviders() as $multiFactorAuthenticationProvider) { + if (! $multiFactorAuthenticationProvider->isEnabled($user)) { + continue; + } + + $this->userUndertakingMultiFactorAuthentication = encrypt($user->getAuthIdentifier()); + + if ($multiFactorAuthenticationProvider instanceof HasBeforeChallengeHook) { + $multiFactorAuthenticationProvider->beforeChallenge($user); + } + + break; + } + + if (filled($this->userUndertakingMultiFactorAuthentication)) { + $this->multiFactorChallengeForm->fill(); + + return null; + } + } + + if (! $authGuard->attemptWhen($credentials, function (Authenticatable $user): bool { + if (! ($user instanceof FilamentUser)) { + return true; + } + + return $user->canAccessPanel(Filament::getCurrentOrDefaultPanel()); + }, $data['remember'] ?? false)) { + $this->fireFailedEvent($authGuard, $user, $credentials); + $this->throwFailureValidationException(); + } + + session()->regenerate(); + + SystemNotification::success( + 'Berhasil Masuk ✨', + 'Selamat datang kembali, '.$user->name.'. Sesi Anda telah berhasil diaktifkan dengan aman.' + )->send(); + + return app(LoginResponse::class); + } + + protected function throwFailureValidationException(): never + { + SystemNotification::danger( + 'Gagal Masuk 🚫', + 'Terjadi kendala saat proses masuk. Mohon periksa kembali alamat surel atau kata sandi Anda.' + )->send(); + + throw ValidationException::withMessages([]); + } +} diff --git a/app/Providers/Filament/AdminPanelProvider.php b/app/Providers/Filament/AdminPanelProvider.php index fea13a7..32266f3 100644 --- a/app/Providers/Filament/AdminPanelProvider.php +++ b/app/Providers/Filament/AdminPanelProvider.php @@ -2,6 +2,7 @@ namespace App\Providers\Filament; +use App\Filament\Pages\Auth\Login; use DiogoGPinto\AuthUIEnhancer\AuthUIEnhancerPlugin; use Filament\Http\Middleware\Authenticate; use Filament\Http\Middleware\AuthenticateSession; @@ -29,7 +30,7 @@ public function panel(Panel $panel): Panel ->id('admin') ->path('admin') ->viteTheme('resources/css/filament/admin/theme.css') - ->login() + ->login(Login::class) ->colors([ 'primary' => Color::Amber, ])