From fdab49fcaff9727fadc6cc20d759f5113e1f89b6 Mon Sep 17 00:00:00 2001 From: Boy132 Date: Thu, 14 Nov 2024 10:40:20 +0100 Subject: [PATCH 1/3] allow login with username --- app/Filament/Pages/Auth/Login.php | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/app/Filament/Pages/Auth/Login.php b/app/Filament/Pages/Auth/Login.php index a4f157a374..b2560fc764 100644 --- a/app/Filament/Pages/Auth/Login.php +++ b/app/Filament/Pages/Auth/Login.php @@ -3,7 +3,10 @@ namespace App\Filament\Pages\Auth; use Coderflex\FilamentTurnstile\Forms\Components\Turnstile; +use Filament\Forms\Components\Component; +use Filament\Forms\Components\TextInput; use Filament\Pages\Auth\Login as BaseLogin; +use Illuminate\Validation\ValidationException; class Login extends BaseLogin { @@ -13,7 +16,7 @@ protected function getForms(): array 'form' => $this->form( $this->makeForm() ->schema([ - $this->getEmailFormComponent(), + $this->getLoginFormComponent(), $this->getPasswordFormComponent(), $this->getRememberFormComponent(), Turnstile::make('captcha') @@ -31,6 +34,28 @@ protected function throwFailureValidationException(): never { $this->dispatch('reset-captcha'); - parent::throwFailureValidationException(); + throw ValidationException::withMessages([ + 'data.login' => __('filament-panels::pages/auth/login.messages.failed'), + ]); + } + + protected function getLoginFormComponent(): Component + { + return TextInput::make('login') + ->label('Login') + ->required() + ->autocomplete() + ->autofocus() + ->extraInputAttributes(['tabindex' => 1]); + } + + protected function getCredentialsFromFormData(array $data): array + { + $loginType = filter_var($data['login'], FILTER_VALIDATE_EMAIL) ? 'email' : 'username'; + + return [ + $loginType => $data['login'], + 'password' => $data['password'], + ]; } } From f0d10aeed9855f6f4c10ff5c4698a67f97804fc3 Mon Sep 17 00:00:00 2001 From: Boy132 Date: Thu, 14 Nov 2024 11:12:49 +0100 Subject: [PATCH 2/3] make login case insensitive --- app/Filament/Pages/Auth/Login.php | 2 +- app/Models/User.php | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/Filament/Pages/Auth/Login.php b/app/Filament/Pages/Auth/Login.php index b2560fc764..d1bf4b007d 100644 --- a/app/Filament/Pages/Auth/Login.php +++ b/app/Filament/Pages/Auth/Login.php @@ -54,7 +54,7 @@ protected function getCredentialsFromFormData(array $data): array $loginType = filter_var($data['login'], FILTER_VALIDATE_EMAIL) ? 'email' : 'username'; return [ - $loginType => $data['login'], + $loginType => mb_strtolower($data['login']), 'password' => $data['password'], ]; } diff --git a/app/Models/User.php b/app/Models/User.php index 733b51d84b..adb2caeb6e 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -252,6 +252,14 @@ public function setUsernameAttribute(string $value): void $this->attributes['username'] = mb_strtolower($value); } + /** + * Store the email as a lowercase string. + */ + public function setEmailAttribute(string $value): void + { + $this->attributes['email'] = mb_strtolower($value); + } + /** * Return a concatenated result for the accounts full name. */ From b159079361eca33bd27d973a6a4e174e897f655a Mon Sep 17 00:00:00 2001 From: Boy132 Date: Fri, 15 Nov 2024 13:54:47 +0100 Subject: [PATCH 3/3] fix tests --- tests/Integration/Api/Client/AccountControllerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/Api/Client/AccountControllerTest.php b/tests/Integration/Api/Client/AccountControllerTest.php index ccca6818d5..d62fc5fafb 100644 --- a/tests/Integration/Api/Client/AccountControllerTest.php +++ b/tests/Integration/Api/Client/AccountControllerTest.php @@ -47,7 +47,7 @@ public function testEmailIsUpdated(): void $user = User::factory()->create(); $response = $this->actingAs($user)->putJson('/api/client/account/email', [ - 'email' => $email = Str::random() . '@example.com', + 'email' => $email = mb_strtolower(Str::random() . '@example.com'), 'password' => 'password', ]);