Skip to content

Commit f999422

Browse files
committed
Implement rate limiting and social login validation in authentication process
1 parent 4e772af commit f999422

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

app/Filament/Pages/Login.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,60 @@
22

33
namespace App\Filament\Pages;
44

5+
use DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException;
6+
use Filament\Facades\Filament;
57
use Filament\Forms\Components\Component;
68
use Filament\Forms\Components\TextInput;
79
use Filament\Pages\Auth\Login as BaseLogin;
810
use Illuminate\Contracts\View\View;
11+
use Filament\Http\Responses\Auth\Contracts\LoginResponse;
12+
use Filament\Models\Contracts\FilamentUser;
13+
use Illuminate\Validation\ValidationException;
914

1015
class Login extends BaseLogin
1116
{
1217
protected static string $view = 'filament.pages.login';
1318

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+
1459
public function mount(): void
1560
{
1661
parent::mount();

0 commit comments

Comments
 (0)