|
2 | 2 |
|
3 | 3 | namespace App\Filament\Pages; |
4 | 4 |
|
| 5 | +use DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException; |
| 6 | +use Filament\Facades\Filament; |
5 | 7 | use Filament\Forms\Components\Component; |
6 | 8 | use Filament\Forms\Components\TextInput; |
7 | 9 | use Filament\Pages\Auth\Login as BaseLogin; |
8 | 10 | use Illuminate\Contracts\View\View; |
| 11 | +use Filament\Http\Responses\Auth\Contracts\LoginResponse; |
| 12 | +use Filament\Models\Contracts\FilamentUser; |
| 13 | +use Illuminate\Validation\ValidationException; |
9 | 14 |
|
10 | 15 | class Login extends BaseLogin |
11 | 16 | { |
12 | 17 | protected static string $view = 'filament.pages.login'; |
13 | 18 |
|
| 19 | + public function authenticate(): ?LoginResponse |
| 20 | + { |
| 21 | + try { |
| 22 | + $this->rateLimit(5); |
| 23 | + } catch (TooManyRequestsException $exception) { |
| 24 | + $this->getRateLimitedNotification($exception)?->send(); |
| 25 | + |
| 26 | + return null; |
| 27 | + } |
| 28 | + |
| 29 | + $data = $this->form->getState(); |
| 30 | + |
| 31 | + // Check if user exists and was created through social login |
| 32 | + $user = \App\Models\User::where('email', $data['email'])->first(); |
| 33 | + if ($user && is_null($user->password)) { |
| 34 | + throw ValidationException::withMessages([ |
| 35 | + 'data.email' => 'This account was created using social login. Please login with Google.', |
| 36 | + ]); |
| 37 | + } |
| 38 | + |
| 39 | + if (! Filament::auth()->attempt($this->getCredentialsFromFormData($data), $data['remember'] ?? false)) { |
| 40 | + $this->throwFailureValidationException(); |
| 41 | + } |
| 42 | + |
| 43 | + $user = Filament::auth()->user(); |
| 44 | + |
| 45 | + if ( |
| 46 | + ($user instanceof FilamentUser) && |
| 47 | + (! $user->canAccessPanel(Filament::getCurrentPanel())) |
| 48 | + ) { |
| 49 | + Filament::auth()->logout(); |
| 50 | + |
| 51 | + $this->throwFailureValidationException(); |
| 52 | + } |
| 53 | + |
| 54 | + session()->regenerate(); |
| 55 | + |
| 56 | + return app(LoginResponse::class); |
| 57 | + } |
| 58 | + |
14 | 59 | public function mount(): void |
15 | 60 | { |
16 | 61 | parent::mount(); |
|
0 commit comments