Skip to content

Commit a3dbdba

Browse files
committed
Add hasSetupData to avoid enabling 2FA again
1 parent 02be899 commit a3dbdba

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

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

Lines changed: 4 additions & 1 deletion
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, useState } from 'react';
3+
import { useCallback, useMemo, useState } from 'react';
44

55
export const OTP_MAX_LENGTH = 6;
66

@@ -21,6 +21,8 @@ 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+
2426
const fetchQrCode = useCallback(async (): Promise<void> => {
2527
try {
2628
const { svg } = await fetchJson<TwoFactorSetupData>(qrCode.url());
@@ -70,6 +72,7 @@ export const useTwoFactorAuth = () => {
7072
qrCodeSvg,
7173
manualSetupKey,
7274
recoveryCodesList,
75+
hasSetupData,
7376
clearSetupData,
7477
fetchQrCode,
7578
fetchSetupKey,

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

Lines changed: 16 additions & 8 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 { qrCodeSvg, manualSetupKey, clearSetupData, fetchSetupData, recoveryCodesList, fetchRecoveryCodes } = useTwoFactorAuth();
28+
const { qrCodeSvg, hasSetupData, manualSetupKey, clearSetupData, fetchSetupData, recoveryCodesList, fetchRecoveryCodes } = useTwoFactorAuth();
2929
const [showSetupModal, setShowSetupModal] = useState<boolean>(false);
3030

3131
return (
@@ -63,13 +63,21 @@ export default function TwoFactor({ requiresConfirmation = false, twoFactorEnabl
6363
</p>
6464

6565
<div>
66-
<Form {...enable.form()} onSuccess={() => setShowSetupModal(true)}>
67-
{({ processing }) => (
68-
<Button type="submit" disabled={processing}>
69-
<ShieldCheck /> Enable 2FA
70-
</Button>
71-
)}
72-
</Form>
66+
{hasSetupData ? (
67+
<Button onClick={() => setShowSetupModal(true)}>
68+
<ShieldCheck />
69+
Continue Setup
70+
</Button>
71+
) : (
72+
<Form {...enable.form()} onSuccess={() => setShowSetupModal(true)}>
73+
{({ processing }) => (
74+
<Button type="submit" disabled={processing}>
75+
<ShieldCheck />
76+
Enable 2FA
77+
</Button>
78+
)}
79+
</Form>
80+
)}
7381
</div>
7482
</div>
7583
)}

0 commit comments

Comments
 (0)