Skip to content

Commit 61ae3c6

Browse files
committed
Add an error state in useTwoFactorAuth.ts
1 parent 243b25d commit 61ae3c6

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

resources/js/composables/useTwoFactorAuth.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,66 @@ const fetchJson = async <T>(url: string): Promise<T> => {
1313
return response.json();
1414
};
1515

16-
const qrCodeSvg = ref<string | null>(null);
16+
const errors = ref<string[]>([]);
1717
const manualSetupKey = ref<string | null>(null);
18+
const qrCodeSvg = ref<string | null>(null);
1819
const recoveryCodesList = ref<string[]>([]);
1920

2021
const hasSetupData = computed<boolean>(() => qrCodeSvg.value !== null && manualSetupKey.value !== null);
2122

2223
export const useTwoFactorAuth = () => {
2324
const fetchQrCode = async (): Promise<void> => {
24-
const { svg } = await fetchJson<{ svg: string; url: string }>(qrCode.url());
25+
try {
26+
const { svg } = await fetchJson<{ svg: string; url: string }>(qrCode.url());
2527

26-
qrCodeSvg.value = svg;
28+
qrCodeSvg.value = svg;
29+
} catch {
30+
errors.value.push('Failed to fetch QR code');
31+
qrCodeSvg.value = null;
32+
}
2733
};
2834

2935
const fetchSetupKey = async (): Promise<void> => {
30-
const { secretKey: key } = await fetchJson<{ secretKey: string }>(secretKey.url());
36+
try {
37+
const { secretKey: key } = await fetchJson<{ secretKey: string }>(secretKey.url());
3138

32-
manualSetupKey.value = key;
39+
manualSetupKey.value = key;
40+
} catch {
41+
errors.value.push('Failed to fetch a setup key');
42+
manualSetupKey.value = null;
43+
}
3344
};
3445

3546
const clearSetupData = (): void => {
3647
manualSetupKey.value = null;
3748
qrCodeSvg.value = null;
49+
clearErrors();
50+
};
51+
52+
const clearErrors = (): void => {
53+
errors.value = [];
3854
};
3955

4056
const clearTwoFactorAuthData = (): void => {
4157
clearSetupData();
42-
58+
clearErrors();
4359
recoveryCodesList.value = [];
4460
};
4561

4662
const fetchRecoveryCodes = async (): Promise<void> => {
4763
try {
64+
clearErrors();
4865
recoveryCodesList.value = await fetchJson<string[]>(recoveryCodes.url());
49-
} catch (error) {
50-
console.error('Failed to fetch recovery codes:', error);
51-
66+
} catch {
67+
errors.value.push('Failed to fetch recovery codes');
5268
recoveryCodesList.value = [];
5369
}
5470
};
5571

5672
const fetchSetupData = async (): Promise<void> => {
5773
try {
5874
await Promise.all([fetchQrCode(), fetchSetupKey()]);
59-
} catch (error) {
60-
console.error('Failed to fetch setup data:', error);
61-
75+
} catch {
6276
qrCodeSvg.value = null;
6377
manualSetupKey.value = null;
6478
}
@@ -68,8 +82,10 @@ export const useTwoFactorAuth = () => {
6882
qrCodeSvg,
6983
manualSetupKey,
7084
recoveryCodesList,
85+
errors,
7186
hasSetupData,
7287
clearSetupData,
88+
clearErrors,
7389
clearTwoFactorAuthData,
7490
fetchQrCode,
7591
fetchSetupKey,

0 commit comments

Comments
 (0)