Skip to content

Commit e5f8b6f

Browse files
committed
Add an error state in use-two-factor-auth.ts
1 parent f779357 commit e5f8b6f

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ export const useTwoFactorAuth = () => {
2828
const [qrCodeSvg, setQrCodeSvg] = useState<string | null>(null);
2929
const [manualSetupKey, setManualSetupKey] = useState<string | null>(null);
3030
const [recoveryCodesList, setRecoveryCodesList] = useState<string[]>([]);
31+
const [errors, setErrors] = useState<{
32+
qrCode?: string;
33+
setupKey?: string;
34+
recoveryCodes?: string;
35+
}>({});
3136

3237
const hasSetupData = useMemo<boolean>(() => qrCodeSvg !== null && manualSetupKey !== null, [qrCodeSvg, manualSetupKey]);
3338

@@ -36,21 +41,18 @@ export const useTwoFactorAuth = () => {
3641
const { svg } = await fetchJson<TwoFactorSetupData>(qrCode.url());
3742

3843
setQrCodeSvg(svg);
39-
} catch (error) {
40-
console.error('Failed to fetch QR code:', error);
41-
44+
} catch {
45+
setErrors(prev => ({ ...prev, qrCode: 'Failed to fetch QR code' }));
4246
setQrCodeSvg(null);
4347
}
4448
}, []);
4549

4650
const fetchSetupKey = useCallback(async (): Promise<void> => {
4751
try {
4852
const { secretKey: key } = await fetchJson<TwoFactorSecretKey>(secretKey.url());
49-
5053
setManualSetupKey(key);
51-
} catch (error) {
52-
console.error('Failed to fetch setup key:', error);
53-
54+
} catch {
55+
setErrors(prev => ({ ...prev, setupKey: 'Failed to fetch a setup key' }));
5456
setManualSetupKey(null);
5557
}
5658
}, []);
@@ -65,19 +67,16 @@ export const useTwoFactorAuth = () => {
6567
const codes = await fetchJson<string[]>(recoveryCodes.url());
6668

6769
setRecoveryCodesList(codes);
68-
} catch (error) {
69-
console.error('Failed to fetch recovery codes:', error);
70-
70+
} catch {
71+
setErrors(prev => ({ ...prev, recoveryCodes: 'Failed to fetch recovery codes' }));
7172
setRecoveryCodesList([]);
7273
}
7374
}, []);
7475

7576
const fetchSetupData = useCallback(async (): Promise<void> => {
7677
try {
7778
await Promise.all([fetchQrCode(), fetchSetupKey()]);
78-
} catch (error) {
79-
console.error('Failed to fetch setup data:', error);
80-
79+
} catch {
8180
setQrCodeSvg(null);
8281
setManualSetupKey(null);
8382
}
@@ -88,6 +87,7 @@ export const useTwoFactorAuth = () => {
8887
manualSetupKey,
8988
recoveryCodesList,
9089
hasSetupData,
90+
errors,
9191
clearSetupData,
9292
fetchQrCode,
9393
fetchSetupKey,

0 commit comments

Comments
 (0)