Skip to content

Commit e44398f

Browse files
committed
Fix: Remove Flash Login credentials. Flashing for 1 request only with Livewire will not work.
1 parent 8964e62 commit e44398f

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

config/filament-2fa.php

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
],
1616

1717
'login' => [
18-
'flashLoginCredentials' => false,
1918
'credential_key' => '_2fa_login',
2019
'confirm_totp_page_url' => 'confirm-2fa'
2120
],

src/Filament/Pages/Login.php

+23-16
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ class Login extends BaseLogin
1616
{
1717
public function authenticate(): null|TwoFactorAuthResponse|LoginResponse
1818
{
19-
if ($response = $this->handleRateLimiting()) {
20-
return $response;
19+
$this->handleRateLimiting();
20+
21+
// Stop execution if there are validation errors
22+
if ($this->getErrorBag()->isNotEmpty()) {
23+
return null;
2124
}
2225

2326
$data = $this->form->getState();
@@ -30,8 +33,11 @@ public function authenticate(): null|TwoFactorAuthResponse|LoginResponse
3033

3134
$user = Filament::auth()->user();
3235

33-
if ($response = $this->handleTwoFactorAuthentication($user, $credentials, $remember)) {
34-
return $response;
36+
if ($this->needsTwoFactorAuthentication($user)) {
37+
$this->storeCredentials($credentials, $remember);
38+
Filament::auth()->logout();
39+
40+
return app(TwoFactorAuthResponse::class);
3541
}
3642

3743
if (! $this->userCanAccessPanel($user)) {
@@ -47,16 +53,20 @@ public function authenticate(): null|TwoFactorAuthResponse|LoginResponse
4753
/**
4854
* Handle rate limiting for login attempts.
4955
*/
50-
protected function handleRateLimiting(): ?LoginResponse
56+
protected function handleRateLimiting(): void
5157
{
5258
try {
5359
$this->rateLimit(5);
5460
} catch (TooManyRequestsException $exception) {
61+
// Optionally send a notification (can be omitted if not needed)
5562
$this->getRateLimitedNotification($exception)?->send();
56-
return null;
57-
}
5863

59-
return null;
64+
// Use the available property to get the number of seconds
65+
$this->addError('email', __('auth.throttle', ['seconds' => $exception->secondsUntilAvailable]));
66+
67+
// Stop further execution by returning early
68+
return;
69+
}
6070
}
6171

6272
/**
@@ -73,7 +83,7 @@ protected function attemptLogin(array $credentials, bool $remember): bool
7383
protected function handleTwoFactorAuthentication(Authenticatable $user, array $credentials, bool $remember): ?TwoFactorAuthResponse
7484
{
7585
if ($this->needsTwoFactorAuthentication($user)) {
76-
$this->flashCredentials($credentials, $remember);
86+
$this->storeCredentials($credentials, $remember);
7787
Filament::auth()->logout();
7888

7989
return app(TwoFactorAuthResponse::class);
@@ -101,9 +111,10 @@ protected function userCanAccessPanel(Authenticatable $user): bool
101111
}
102112

103113
/**
104-
* Flash the credentials into the session, encrypted.
114+
* Save credentials to session encrypted.
115+
* Flash not possible with Filament as Livewire call would clear them before they can be used
105116
*/
106-
protected function flashCredentials(array $credentials, bool $remember): void
117+
protected function storeCredentials(array $credentials, bool $remember): void
107118
{
108119
$encryptedCredentials = array_map(
109120
fn($value) => Crypt::encryptString($value),
@@ -117,10 +128,6 @@ protected function flashCredentials(array $credentials, bool $remember): void
117128

118129
$credentialKey = config('filament-2fa.login.credential_key');
119130

120-
if (config('filament-2fa.login.flashLoginCredentials')) {
121-
request()->session()->flash($credentialKey, $sessionData);
122-
} else {
123-
session([$credentialKey => $sessionData]);
124-
}
131+
session([$credentialKey => $sessionData]);
125132
}
126133
}

0 commit comments

Comments
 (0)