Skip to content

Commit 6754109

Browse files
committed
Use ensure state is valid from Fortify
1 parent ce1e273 commit 6754109

File tree

3 files changed

+5
-61
lines changed

3 files changed

+5
-61
lines changed

app/Http/Controllers/Settings/TwoFactorAuthenticationController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static function middleware(): array
2727
*/
2828
public function show(TwoFactorAuthenticationRequest $request): Response
2929
{
30-
$request->validateState();
30+
$request->ensureStateIsValid();
3131

3232
return Inertia::render('settings/TwoFactor', [
3333
'twoFactorEnabled' => $request->user()->hasEnabledTwoFactorAuthentication(),

app/Http/Requests/Auth/TwoFactorAuthenticationRequest.php

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
namespace App\Http\Requests\Auth;
44

55
use Illuminate\Foundation\Http\FormRequest;
6-
use Illuminate\Support\Facades\Auth;
7-
use Laravel\Fortify\Actions\DisableTwoFactorAuthentication;
86
use Laravel\Fortify\Features;
7+
use Laravel\Fortify\InteractsWithTwoFactorState;
98

109
class TwoFactorAuthenticationRequest extends FormRequest
1110
{
11+
use InteractsWithTwoFactorState;
12+
1213
/**
1314
* Determine if the user is authorized to make this request.
1415
*/
@@ -26,61 +27,4 @@ public function rules(): array
2627
{
2728
return [];
2829
}
29-
30-
/**
31-
* Validate the two-factor authentication state for the request.
32-
*/
33-
public function validateState(): void
34-
{
35-
if (! Features::optionEnabled(Features::twoFactorAuthentication(), 'confirm')) {
36-
return;
37-
}
38-
39-
$currentTime = time();
40-
41-
if ($this->twoFactorAuthenticationDisabled()) {
42-
$this->session()->put('two_factor_empty_at', $currentTime);
43-
}
44-
45-
if ($this->hasJustBegunConfirmingTwoFactorAuthentication()) {
46-
$this->session()->put('two_factor_confirming_at', $currentTime);
47-
}
48-
49-
if ($this->neverFinishedConfirmingTwoFactorAuthentication($currentTime)) {
50-
app(DisableTwoFactorAuthentication::class)(Auth::user());
51-
52-
$this->session()->put('two_factor_empty_at', $currentTime);
53-
$this->session()->remove('two_factor_confirming_at');
54-
}
55-
}
56-
57-
/**
58-
* Determine if two-factor authentication is totally disabled.
59-
*/
60-
protected function twoFactorAuthenticationDisabled(): bool
61-
{
62-
return is_null($this->user()->two_factor_secret) &&
63-
is_null($this->user()->two_factor_confirmed_at);
64-
}
65-
66-
/**
67-
* Determine if two-factor authentication is being confirmed within the last request cycle.
68-
*/
69-
protected function hasJustBegunConfirmingTwoFactorAuthentication(): bool
70-
{
71-
return ! is_null($this->user()->two_factor_secret) &&
72-
is_null($this->user()->two_factor_confirmed_at) &&
73-
$this->session()->has('two_factor_empty_at') &&
74-
is_null($this->session()->get('two_factor_confirming_at'));
75-
}
76-
77-
/**
78-
* Determine if two-factor authentication was never totally confirmed once confirmation started.
79-
*/
80-
protected function neverFinishedConfirmingTwoFactorAuthentication(int $currentTime): bool
81-
{
82-
return ! array_key_exists('code', $this->session()->getOldInput()) &&
83-
is_null($this->user()->two_factor_confirmed_at) &&
84-
$this->session()->get('two_factor_confirming_at', 0) != $currentTime;
85-
}
8630
}

tests/Feature/Auth/PasswordResetTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function test_reset_password_screen_can_be_rendered()
3939
$this->post(route('password.email'), ['email' => $user->email]);
4040

4141
Notification::assertSentTo($user, ResetPassword::class, function ($notification) use ($user) {
42-
$response = $this->get(route('password.reset', $notification->token) . '?email=' . $user->email);
42+
$response = $this->get(route('password.reset', $notification->token).'?email='.$user->email);
4343

4444
$response->assertStatus(200)
4545
->assertInertia(fn ($page) => $page

0 commit comments

Comments
 (0)