Files
core/src/Auth/Filament/Pages/Login.php
T

104 lines
2.6 KiB
PHP
Raw Normal View History

2026-07-01 18:35:39 +03:00
<?php
namespace Modules\Core\Auth\Filament\Pages;
use DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException;
use DanHarrin\LivewireRateLimiting\WithRateLimiting;
use Filament\Facades\Filament;
use Filament\Models\Contracts\FilamentUser;
use Filament\Pages\SimplePage;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Validation\ValidationException;
use Modules\Core\Auth\Services\OtpService;
class Login extends SimplePage
{
use WithRateLimiting;
protected static string $view = 'core::auth.filament.pages.login';
public ?string $email = '';
public ?string $otp = '';
public bool $otpSent = false;
private OtpService $otpService;
public function boot(OtpService $otpService): void
{
$this->otpService = $otpService;
}
public function mount(): void
{
if (Filament::auth()->check()) {
redirect()->intended(Filament::getUrl());
}
}
public function back(): void
{
$this->otpSent = false;
$this->otp = '';
}
2026-07-01 18:35:39 +03:00
public function requestOtp(): void
{
$this->validate(['email' => 'required|email']);
try {
$this->rateLimit(5);
} catch (TooManyRequestsException) {
throw ValidationException::withMessages([
'email' => 'Too many attempts. Please wait before trying again.',
]);
}
$this->otpService->generateAndSend($this->email);
$this->otpSent = true;
}
public function authenticate(): void
{
$this->validate(['otp' => 'required']);
try {
$this->rateLimit(5);
} catch (TooManyRequestsException) {
throw ValidationException::withMessages([
'otp' => 'Too many attempts. Please wait before trying again.',
]);
}
$staff = $this->otpService->validate($this->email, $this->otp);
if (!$staff) {
throw ValidationException::withMessages([
'otp' => 'Invalid or expired code.',
]);
}
if ($staff instanceof FilamentUser && !$staff->canAccessPanel(Filament::getCurrentPanel())) {
throw ValidationException::withMessages([
'email' => 'You do not have access to this panel.',
]);
}
Filament::auth()->login($staff);
session()->regenerate();
redirect()->intended(Filament::getUrl());
}
public function getTitle(): string | Htmlable
{
return 'Sign in';
}
public function getHeading(): string | Htmlable
{
return 'Sign in to your account';
}
}