Skip to content

Commit db3dc97

Browse files
committed
formating
1 parent 5397f80 commit db3dc97

File tree

8 files changed

+21
-209
lines changed

8 files changed

+21
-209
lines changed

resources/js/components/two-factor-recovery-codes.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ export default function TwoFactorRecoveryCodes({ recoveryCodesList, fetchRecover
6161
<Form {...regenerateRecoveryCodes.form()} options={{ preserveScroll: true }} onSuccess={fetchRecoveryCodes}>
6262
{({ processing }) => (
6363
<Button variant="secondary" type="submit" disabled={processing} aria-describedby="regenerate-warning">
64-
<RefreshCw className={`mr-2 size-4 ${processing ? 'animate-spin' : ''}`} aria-hidden="true" />
65-
{processing ? 'Regenerating...' : 'Regenerate Codes'}
64+
<RefreshCw /> Regenerate Codes
6665
</Button>
6766
)}
6867
</Form>

resources/js/components/two-factor-setup-modal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ function TwoFactorVerificationStep({ onClose, onBack }: { onClose: () => void; o
134134
Back
135135
</Button>
136136
<Button type="submit" className="flex-1" disabled={processing || code.length < OTP_MAX_LENGTH}>
137-
{processing ? 'Confirming...' : 'Confirm'}
137+
Confirm
138138
</Button>
139139
</div>
140140
</div>

resources/js/hooks/use-two-factor-auth.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { qrCode, recoveryCodes, secretKey } from '@/routes/two-factor';
22
import { type TwoFactorSecretKey, type TwoFactorSetupData } from '@/types';
3-
import { useCallback, useMemo, useState } from 'react';
3+
import { useCallback, useState } from 'react';
44

55
export const OTP_MAX_LENGTH = 6;
66

@@ -21,8 +21,6 @@ export const useTwoFactorAuth = () => {
2121
const [manualSetupKey, setManualSetupKey] = useState<string | null>(null);
2222
const [recoveryCodesList, setRecoveryCodesList] = useState<string[]>([]);
2323

24-
const hasSetupData = useMemo<boolean>(() => qrCodeSvg !== null && manualSetupKey !== null, [qrCodeSvg, manualSetupKey]);
25-
2624
const fetchQrCode = useCallback(async (): Promise<void> => {
2725
try {
2826
const { svg } = await fetchJson<TwoFactorSetupData>(qrCode.url());
@@ -72,7 +70,6 @@ export const useTwoFactorAuth = () => {
7270
qrCodeSvg,
7371
manualSetupKey,
7472
recoveryCodesList,
75-
hasSetupData,
7673
clearSetupData,
7774
fetchQrCode,
7875
fetchSetupKey,

resources/js/pages/settings/two-factor.tsx

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const breadcrumbs: BreadcrumbItem[] = [
2525
];
2626

2727
export default function TwoFactor({ requiresConfirmation = false, twoFactorEnabled = false }: TwoFactorProps) {
28-
const { hasSetupData, qrCodeSvg, manualSetupKey, clearSetupData, fetchSetupData, recoveryCodesList, fetchRecoveryCodes } = useTwoFactorAuth();
28+
const { qrCodeSvg, manualSetupKey, clearSetupData, fetchSetupData, recoveryCodesList, fetchRecoveryCodes } = useTwoFactorAuth();
2929
const [showSetupModal, setShowSetupModal] = useState<boolean>(false);
3030

3131
return (
@@ -44,21 +44,13 @@ export default function TwoFactor({ requiresConfirmation = false, twoFactorEnabl
4444
</p>
4545

4646
<div>
47-
{hasSetupData ? (
48-
<Button onClick={() => setShowSetupModal(true)}>
49-
<ShieldCheck />
50-
Continue Setup
51-
</Button>
52-
) : (
53-
<Form {...enable.form()} onSuccess={() => setShowSetupModal(true)}>
54-
{({ processing }) => (
55-
<Button type="submit" disabled={processing}>
56-
<ShieldCheck />
57-
{processing ? 'Enabling...' : 'Enable 2FA'}
58-
</Button>
59-
)}
60-
</Form>
61-
)}
47+
<Form {...enable.form()} onSuccess={() => setShowSetupModal(true)}>
48+
{({ processing }) => (
49+
<Button type="submit" disabled={processing}>
50+
<ShieldCheck /> Enable 2FA
51+
</Button>
52+
)}
53+
</Form>
6254
</div>
6355
</div>
6456
) : (
@@ -75,8 +67,7 @@ export default function TwoFactor({ requiresConfirmation = false, twoFactorEnabl
7567
<Form {...disable.form()}>
7668
{({ processing }) => (
7769
<Button variant="destructive" type="submit" disabled={processing}>
78-
<ShieldBan />
79-
{processing ? 'Disabling...' : 'Disable 2FA'}
70+
<ShieldBan /> Disable 2FA
8071
</Button>
8172
)}
8273
</Form>

tests/Feature/Auth/AuthenticationTest.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Models\User;
66
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use Illuminate\Support\Facades\RateLimiter;
78
use Laravel\Fortify\Features;
89
use Tests\TestCase;
910

@@ -60,20 +61,6 @@ public function test_users_with_two_factor_enabled_are_redirected_to_two_factor_
6061
$this->assertGuest();
6162
}
6263

63-
public function test_users_without_two_factor_enabled_login_normally()
64-
{
65-
$user = User::factory()->create();
66-
67-
$response = $this->post(route('login'), [
68-
'email' => $user->email,
69-
'password' => 'password',
70-
]);
71-
72-
$this->assertAuthenticated();
73-
$response->assertRedirect(route('dashboard', absolute: false));
74-
$response->assertSessionMissing('login.id');
75-
}
76-
7764
public function test_users_can_not_authenticate_with_invalid_password()
7865
{
7966
$user = User::factory()->create();
@@ -100,14 +87,7 @@ public function test_users_are_rate_limited()
10087
{
10188
$user = User::factory()->create();
10289

103-
for ($i = 0; $i < 5; $i++) {
104-
$this->post(route('login.store'), [
105-
'email' => $user->email,
106-
'password' => 'wrong-password',
107-
])->assertStatus(302)->assertSessionHasErrors([
108-
'email' => 'These credentials do not match our records.',
109-
]);
110-
}
90+
RateLimiter::increment(implode('|', [$user->email, '127.0.0.1']), amount: 10);
11191

11292
$response = $this->post(route('login.store'), [
11393
'email' => $user->email,

tests/Feature/Auth/PasswordConfirmationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function test_confirm_password_screen_can_be_rendered()
1818
$response = $this->actingAs($user)->get(route('password.confirm'));
1919

2020
$response->assertStatus(200);
21+
2122
$response->assertInertia(fn (Assert $page) => $page
2223
->component('auth/confirm-password')
2324
);

tests/Feature/Auth/TwoFactorChallengeTest.php

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class TwoFactorChallengeTest extends TestCase
1212
{
1313
use RefreshDatabase;
1414

15-
public function test_two_factor_challenge_redirects_when_not_authenticated(): void
15+
public function test_two_factor_challenge_redirects_to_login_when_not_authenticated(): void
1616
{
1717
if (! Features::canManageTwoFactorAuthentication()) {
1818
$this->markTestSkipped('Two-factor authentication is not enabled.');
@@ -23,7 +23,7 @@ public function test_two_factor_challenge_redirects_when_not_authenticated(): vo
2323
$response->assertRedirect(route('login'));
2424
}
2525

26-
public function test_two_factor_challenge_renders_correct_inertia_component(): void
26+
public function test_two_factor_challenge_can_be_rendered(): void
2727
{
2828
if (! Features::canManageTwoFactorAuthentication()) {
2929
$this->markTestSkipped('Two-factor authentication is not enabled.');
@@ -53,41 +53,4 @@ public function test_two_factor_challenge_renders_correct_inertia_component(): v
5353
->component('auth/two-factor-challenge')
5454
);
5555
}
56-
57-
public function test_two_factor_authentication_is_rate_limited(): void
58-
{
59-
if (! Features::enabled(Features::twoFactorAuthentication())) {
60-
$this->markTestSkipped('Two-factor authentication is not enabled.');
61-
}
62-
63-
Features::twoFactorAuthentication([
64-
'confirm' => true,
65-
'confirmPassword' => true,
66-
]);
67-
68-
$user = User::factory()->create();
69-
70-
$user->forceFill([
71-
'two_factor_secret' => encrypt(implode(range('A', 'P'))),
72-
'two_factor_recovery_codes' => encrypt(json_encode(['recovery-code-1', 'recovery-code-2'])),
73-
'two_factor_confirmed_at' => now(),
74-
])->save();
75-
76-
$this->post(route('login'), [
77-
'email' => $user->email,
78-
'password' => 'password',
79-
]);
80-
81-
foreach (range(0, 4) as $ignored) {
82-
$this->post(route('two-factor.login.store'), [
83-
'code' => '000000',
84-
])->assertSessionHasErrors('code');
85-
}
86-
87-
$response = $this->post(route('two-factor.login.store'), [
88-
'code' => '000000',
89-
]);
90-
91-
$response->assertTooManyRequests();
92-
}
9356
}

tests/Feature/Settings/TwoFactorAuthenticationTest.php

Lines changed: 4 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class TwoFactorAuthenticationTest extends TestCase
1212
{
1313
use RefreshDatabase;
1414

15-
public function test_two_factor_settings_page_is_displayed()
15+
public function test_two_factor_settings_page_can_be_rendered()
1616
{
1717
if (! Features::canManageTwoFactorAuthentication()) {
1818
$this->markTestSkipped('Two-factor authentication is not enabled.');
@@ -34,7 +34,7 @@ public function test_two_factor_settings_page_is_displayed()
3434
);
3535
}
3636

37-
public function test_two_factor_settings_page_requires_password_confirmation()
37+
public function test_two_factor_settings_page_requires_password_confirmation_when_enabled()
3838
{
3939
if (! Features::canManageTwoFactorAuthentication()) {
4040
$this->markTestSkipped('Two-factor authentication is not enabled.');
@@ -53,7 +53,7 @@ public function test_two_factor_settings_page_requires_password_confirmation()
5353
$response->assertRedirect(route('password.confirm'));
5454
}
5555

56-
public function test_two_factor_settings_page_does_not_requires_password_confirmation_if_that_feature_is_disabled()
56+
public function test_two_factor_settings_page_does_not_requires_password_confirmation_when_disabled()
5757
{
5858
if (! Features::canManageTwoFactorAuthentication()) {
5959
$this->markTestSkipped('Two-factor authentication is not enabled.');
@@ -74,7 +74,7 @@ public function test_two_factor_settings_page_does_not_requires_password_confirm
7474
);
7575
}
7676

77-
public function test_two_factor_settings_page_returns_forbidden_when_two_factor_is_disabled()
77+
public function test_two_factor_settings_page_returns_forbidden_response_when_two_factor_is_disabled()
7878
{
7979
if (! Features::canManageTwoFactorAuthentication()) {
8080
$this->markTestSkipped('Two-factor authentication is not enabled.');
@@ -89,123 +89,4 @@ public function test_two_factor_settings_page_returns_forbidden_when_two_factor_
8989
->get(route('two-factor.show'))
9090
->assertForbidden();
9191
}
92-
93-
public function test_controller_sets_confirming_data_when_enabling_two_factor_with_confirmation()
94-
{
95-
if (! Features::canManageTwoFactorAuthentication()) {
96-
$this->markTestSkipped('Two-factor authentication is not enabled.');
97-
}
98-
99-
Features::twoFactorAuthentication([
100-
'confirm' => true,
101-
'confirmPassword' => false,
102-
]);
103-
104-
$user = User::factory()->create();
105-
106-
$this->actingAs($user)
107-
->withSession(['auth.password_confirmed_at' => time()])
108-
->withSession(['two_factor_empty_at' => time() - 10])
109-
->post(route('two-factor.enable'));
110-
111-
$this->get(route('two-factor.show'))
112-
->assertOk();
113-
114-
$this->assertNotNull(session('two_factor_confirming_at'));
115-
}
116-
117-
public function test_user_can_view_setting_page_when_confirm_disabled()
118-
{
119-
if (! Features::canManageTwoFactorAuthentication()) {
120-
$this->markTestSkipped('Two-factor authentication is not enabled.');
121-
}
122-
123-
Features::twoFactorAuthentication([
124-
'confirm' => false,
125-
'confirmPassword' => false,
126-
]);
127-
128-
$user = User::factory()->create();
129-
130-
$this->actingAs($user)
131-
->get(route('two-factor.show'))
132-
->assertOk()
133-
->assertInertia(fn (Assert $page) => $page
134-
->component('settings/two-factor')
135-
->where('requiresConfirmation', false)
136-
);
137-
}
138-
139-
public function test_controller_sets_empty_session_data_when_transitioning_to_disabled_state()
140-
{
141-
if (! Features::canManageTwoFactorAuthentication()) {
142-
$this->markTestSkipped('Two-factor authentication is not enabled.');
143-
}
144-
145-
Features::twoFactorAuthentication([
146-
'confirm' => true,
147-
'confirmPassword' => false,
148-
]);
149-
150-
$user = User::factory()->create();
151-
152-
$this->actingAs($user)
153-
->get(route('two-factor.show'))
154-
->assertSessionHas('two_factor_empty_at');
155-
}
156-
157-
public function test_controller_removes_confirming_session_data_when_cleanup_triggered()
158-
{
159-
if (! Features::canManageTwoFactorAuthentication()) {
160-
$this->markTestSkipped('Two-factor authentication is not enabled.');
161-
}
162-
163-
Features::twoFactorAuthentication([
164-
'confirm' => true,
165-
'confirmPassword' => false,
166-
]);
167-
168-
$user = User::factory()->create();
169-
170-
$user->forceFill([
171-
'two_factor_secret' => encrypt('test-secret'),
172-
'two_factor_recovery_codes' => encrypt(json_encode(['code1', 'code2'])),
173-
])->save();
174-
175-
$this->actingAs($user)
176-
->withSession(['two_factor_confirming_at' => time() - 100])
177-
->get(route('two-factor.show'))
178-
->assertSessionMissing('two_factor_confirming_at')
179-
->assertSessionHas('two_factor_empty_at');
180-
}
181-
182-
public function test_two_factor_authentication_disabled_when_confirmation_abandoned_between_requests()
183-
{
184-
if (! Features::canManageTwoFactorAuthentication()) {
185-
$this->markTestSkipped('Two-factor authentication is not enabled.');
186-
}
187-
188-
Features::twoFactorAuthentication([
189-
'confirm' => true,
190-
'confirmPassword' => false,
191-
]);
192-
193-
$user = User::factory()->create();
194-
195-
$user->forceFill([
196-
'two_factor_secret' => encrypt('test-secret'),
197-
'two_factor_recovery_codes' => encrypt(json_encode(['code1', 'code2'])),
198-
'two_factor_confirmed_at' => null,
199-
])->save();
200-
201-
$this->actingAs($user)
202-
->withSession(['two_factor_confirming_at' => time() - 100])
203-
->get(route('two-factor.show'));
204-
205-
$this->assertDatabaseHas('users', [
206-
'id' => $user->id,
207-
'two_factor_secret' => null,
208-
'two_factor_recovery_codes' => null,
209-
]);
210-
}
21192
}

0 commit comments

Comments
 (0)