87 lines
2.4 KiB
PHP
87 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Pages\Auth;
|
|
|
|
use Filament\Forms\Components\Checkbox;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Forms\Concerns\InteractsWithForms;
|
|
use Filament\Forms\Contracts\HasForms;
|
|
use Filament\Pages\Page;
|
|
use Filament\Actions\Action;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class Login extends Page implements HasForms
|
|
{
|
|
use InteractsWithForms;
|
|
|
|
protected string $view = 'filament.pages.auth.login';
|
|
|
|
public function getFormSchema(): array
|
|
{
|
|
return [
|
|
TextInput::make('data.username_or_email')
|
|
->label('Username or Email')
|
|
->required()
|
|
->autofocus(),
|
|
TextInput::make('data.password')
|
|
->password()
|
|
->required()
|
|
->extraAttributes(['tabindex' => 2]),
|
|
Checkbox::make('data.remember')
|
|
->label('Remember me'),
|
|
];
|
|
}
|
|
|
|
public function submit(): void
|
|
{
|
|
$data = $this->form->getState();
|
|
|
|
$credentials = $this->getCredentialsFromFormData($data);
|
|
|
|
if (! Auth::attempt($credentials, $data['remember'] ?? false)) {
|
|
throw ValidationException::withMessages([
|
|
'data.username_or_email' => __('auth.failed'),
|
|
]);
|
|
}
|
|
|
|
$user = Auth::user();
|
|
|
|
if (! $user->email_verified_at) {
|
|
Auth::logout();
|
|
|
|
throw ValidationException::withMessages([
|
|
'data.username_or_email' => 'Your email address is not verified. Please check your email for a verification link.',
|
|
]);
|
|
}
|
|
|
|
if (! $user->tenant) {
|
|
Auth::logout();
|
|
|
|
throw ValidationException::withMessages([
|
|
'data.username_or_email' => 'No tenant associated with your account. Contact support.',
|
|
]);
|
|
}
|
|
|
|
session()->regenerate();
|
|
|
|
$this->redirect($this->getRedirectUrl());
|
|
}
|
|
|
|
protected function getCredentialsFromFormData(array $data): array
|
|
{
|
|
$usernameOrEmail = $data['username_or_email'];
|
|
$password = $data['password'];
|
|
|
|
$credentials = ['password' => $password];
|
|
|
|
if (filter_var($usernameOrEmail, FILTER_VALIDATE_EMAIL)) {
|
|
$credentials['email'] = $usernameOrEmail;
|
|
} else {
|
|
$credentials['username'] = $usernameOrEmail;
|
|
}
|
|
|
|
return $credentials;
|
|
}
|
|
|
|
} |