@@ -13,52 +13,66 @@ const fetchJson = async <T>(url: string): Promise<T> => {
13
13
return response . json ( ) ;
14
14
} ;
15
15
16
- const qrCodeSvg = ref < string | null > ( null ) ;
16
+ const errors = ref < string [ ] > ( [ ] ) ;
17
17
const manualSetupKey = ref < string | null > ( null ) ;
18
+ const qrCodeSvg = ref < string | null > ( null ) ;
18
19
const recoveryCodesList = ref < string [ ] > ( [ ] ) ;
19
20
20
21
const hasSetupData = computed < boolean > ( ( ) => qrCodeSvg . value !== null && manualSetupKey . value !== null ) ;
21
22
22
23
export const useTwoFactorAuth = ( ) => {
23
24
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 ( ) ) ;
25
27
26
- qrCodeSvg . value = svg ;
28
+ qrCodeSvg . value = svg ;
29
+ } catch {
30
+ errors . value . push ( 'Failed to fetch QR code' ) ;
31
+ qrCodeSvg . value = null ;
32
+ }
27
33
} ;
28
34
29
35
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 ( ) ) ;
31
38
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
+ }
33
44
} ;
34
45
35
46
const clearSetupData = ( ) : void => {
36
47
manualSetupKey . value = null ;
37
48
qrCodeSvg . value = null ;
49
+ clearErrors ( ) ;
50
+ } ;
51
+
52
+ const clearErrors = ( ) : void => {
53
+ errors . value = [ ] ;
38
54
} ;
39
55
40
56
const clearTwoFactorAuthData = ( ) : void => {
41
57
clearSetupData ( ) ;
42
-
58
+ clearErrors ( ) ;
43
59
recoveryCodesList . value = [ ] ;
44
60
} ;
45
61
46
62
const fetchRecoveryCodes = async ( ) : Promise < void > => {
47
63
try {
64
+ clearErrors ( ) ;
48
65
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' ) ;
52
68
recoveryCodesList . value = [ ] ;
53
69
}
54
70
} ;
55
71
56
72
const fetchSetupData = async ( ) : Promise < void > => {
57
73
try {
58
74
await Promise . all ( [ fetchQrCode ( ) , fetchSetupKey ( ) ] ) ;
59
- } catch ( error ) {
60
- console . error ( 'Failed to fetch setup data:' , error ) ;
61
-
75
+ } catch {
62
76
qrCodeSvg . value = null ;
63
77
manualSetupKey . value = null ;
64
78
}
@@ -68,8 +82,10 @@ export const useTwoFactorAuth = () => {
68
82
qrCodeSvg,
69
83
manualSetupKey,
70
84
recoveryCodesList,
85
+ errors,
71
86
hasSetupData,
72
87
clearSetupData,
88
+ clearErrors,
73
89
clearTwoFactorAuthData,
74
90
fetchQrCode,
75
91
fetchSetupKey,
0 commit comments