88 lines
2.4 KiB
PHP
88 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Customer\Pages;
|
|
|
|
use DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException;
|
|
use Filament\Facades\Filament;
|
|
use Filament\Forms\Components\Component;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Http\Responses\Auth\Contracts\LoginResponse;
|
|
use Filament\Models\Contracts\FilamentUser;
|
|
use Filament\Pages\Auth\Login;
|
|
use Illuminate\Contracts\Support\Htmlable;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class CustomerLogin extends Login
|
|
{
|
|
protected function getEmailFormComponent(): Component
|
|
{
|
|
return TextInput::make('username')
|
|
->label('Username')
|
|
->required()
|
|
->autofocus()
|
|
->extraInputAttributes(['tabindex' => 1])
|
|
->autocomplete();
|
|
}
|
|
|
|
protected function getCredentialsFromFormData(array $data): array
|
|
{
|
|
return [
|
|
'username' => $data['username'],
|
|
'password' => $data['password'],
|
|
];
|
|
}
|
|
|
|
protected function throwFailureValidationException(): never
|
|
{
|
|
throw ValidationException::withMessages([
|
|
'data.username' => __('filament-panels::pages/auth/login.messages.failed'),
|
|
]);
|
|
}
|
|
|
|
public function authenticate(): ?LoginResponse
|
|
{
|
|
try {
|
|
$this->rateLimit(5);
|
|
} catch (TooManyRequestsException $exception) {
|
|
$this->getRateLimitedNotification($exception)?->send();
|
|
|
|
return null;
|
|
}
|
|
|
|
$data = $this->form->getState();
|
|
|
|
if (! Filament::auth()->attempt($this->getCredentialsFromFormData($data), $data['remember'] ?? false)) {
|
|
$this->throwFailureValidationException();
|
|
}
|
|
|
|
$user = Filament::auth()->user();
|
|
|
|
if (($user instanceof FilamentUser) && (! $user->canAccessPanel(Filament::getCurrentPanel()))) {
|
|
Filament::auth()->logout();
|
|
|
|
$this->throwFailureValidationException();
|
|
} elseif ($user->customer_id === null) {
|
|
Filament::auth()->logout();
|
|
|
|
throw ValidationException::withMessages([
|
|
'data.username' => 'Incorrect username or password.',
|
|
]);
|
|
}
|
|
|
|
session()->regenerate();
|
|
|
|
return app(LoginResponse::class);
|
|
}
|
|
|
|
public function getTitle(): Htmlable|string
|
|
{
|
|
return __('Login');
|
|
|
|
}
|
|
|
|
public function getHeading(): Htmlable|string
|
|
{
|
|
return __('Login');
|
|
}
|
|
}
|