Skip to content

Commit

Permalink
Allow username on filament login page + make case insensitive (#714)
Browse files Browse the repository at this point in the history
* allow login with username

* make login case insensitive

* fix tests
  • Loading branch information
Boy132 authored Nov 15, 2024
1 parent 24eb52f commit 408897c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
29 changes: 27 additions & 2 deletions app/Filament/Pages/Auth/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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')
Expand All @@ -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 => mb_strtolower($data['login']),
'password' => $data['password'],
];
}
}
8 changes: 8 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Api/Client/AccountControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);

Expand Down

0 comments on commit 408897c

Please sign in to comment.