You've already forked Epicnabbo-Catalogus-Updated-Daily
107 lines
3.3 KiB
PHP
107 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Pages;
|
|
|
|
use DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException;
|
|
use Filament\Auth\Http\Responses\Contracts\LoginResponse;
|
|
use Filament\Facades\Filament;
|
|
use Filament\Forms\Components\Checkbox;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Models\Contracts\FilamentUser;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Schemas\Components\Component;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class Login extends \Filament\Auth\Pages\Login
|
|
{
|
|
public $username = '';
|
|
|
|
public function authenticate(): ?LoginResponse
|
|
{
|
|
try {
|
|
$this->rateLimit(5);
|
|
} catch (TooManyRequestsException $exception) {
|
|
Notification::make()
|
|
->title(__('filament-panels::pages/auth/login.notifications.throttled.title', [
|
|
'seconds' => $exception->secondsUntilAvailable,
|
|
'minutes' => ceil($exception->secondsUntilAvailable / 60),
|
|
]))
|
|
->body(array_key_exists('body', __('filament-panels::pages/auth/login.notifications.throttled') ?: []) ? __('filament-panels::pages/auth/login.notifications.throttled.body', [
|
|
'seconds' => $exception->secondsUntilAvailable,
|
|
'minutes' => ceil($exception->secondsUntilAvailable / 60),
|
|
]) : null)
|
|
->danger()
|
|
->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::getCurrentOrDefaultPanel()))
|
|
) {
|
|
Filament::auth()->logout();
|
|
|
|
$this->throwFailureValidationException();
|
|
}
|
|
|
|
session()->regenerate();
|
|
|
|
return app(LoginResponse::class);
|
|
}
|
|
|
|
protected function throwFailureValidationException(): never
|
|
{
|
|
throw ValidationException::withMessages([
|
|
'data.username' => __('filament-panels::pages/auth/login.messages.failed'),
|
|
]);
|
|
}
|
|
|
|
protected function getFormSchema(): array
|
|
{
|
|
return [
|
|
TextInput::make('username')
|
|
->label(__('filament::login.fields.username.label'))
|
|
->required()
|
|
->autocomplete(),
|
|
TextInput::make('password')
|
|
->label(__('filament::login.fields.password.label'))
|
|
->password()
|
|
->required(),
|
|
Checkbox::make('remember')
|
|
->label(__('filament::login.fields.remember.label')),
|
|
];
|
|
}
|
|
|
|
protected function getEmailFormComponent(): Component
|
|
{
|
|
return TextInput::make('username')
|
|
->label(__('filament::login.fields.username.label'))
|
|
->required()
|
|
->autocomplete()
|
|
->autofocus()
|
|
->extraInputAttributes(['tabindex' => 1]);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $data
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
protected function getCredentialsFromFormData(array $data): array
|
|
{
|
|
return [
|
|
'username' => $data['username'],
|
|
'password' => $data['password'],
|
|
];
|
|
}
|
|
}
|