Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow username on filament login page + make case insensitive #714

Merged
merged 3 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading