@@ -28,6 +28,11 @@ export const useTwoFactorAuth = () => {
28
28
const [ qrCodeSvg , setQrCodeSvg ] = useState < string | null > ( null ) ;
29
29
const [ manualSetupKey , setManualSetupKey ] = useState < string | null > ( null ) ;
30
30
const [ recoveryCodesList , setRecoveryCodesList ] = useState < string [ ] > ( [ ] ) ;
31
+ const [ errors , setErrors ] = useState < {
32
+ qrCode ?: string ;
33
+ setupKey ?: string ;
34
+ recoveryCodes ?: string ;
35
+ } > ( { } ) ;
31
36
32
37
const hasSetupData = useMemo < boolean > ( ( ) => qrCodeSvg !== null && manualSetupKey !== null , [ qrCodeSvg , manualSetupKey ] ) ;
33
38
@@ -36,21 +41,18 @@ export const useTwoFactorAuth = () => {
36
41
const { svg } = await fetchJson < TwoFactorSetupData > ( qrCode . url ( ) ) ;
37
42
38
43
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' } ) ) ;
42
46
setQrCodeSvg ( null ) ;
43
47
}
44
48
} , [ ] ) ;
45
49
46
50
const fetchSetupKey = useCallback ( async ( ) : Promise < void > => {
47
51
try {
48
52
const { secretKey : key } = await fetchJson < TwoFactorSecretKey > ( secretKey . url ( ) ) ;
49
-
50
53
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' } ) ) ;
54
56
setManualSetupKey ( null ) ;
55
57
}
56
58
} , [ ] ) ;
@@ -65,19 +67,16 @@ export const useTwoFactorAuth = () => {
65
67
const codes = await fetchJson < string [ ] > ( recoveryCodes . url ( ) ) ;
66
68
67
69
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' } ) ) ;
71
72
setRecoveryCodesList ( [ ] ) ;
72
73
}
73
74
} , [ ] ) ;
74
75
75
76
const fetchSetupData = useCallback ( async ( ) : Promise < void > => {
76
77
try {
77
78
await Promise . all ( [ fetchQrCode ( ) , fetchSetupKey ( ) ] ) ;
78
- } catch ( error ) {
79
- console . error ( 'Failed to fetch setup data:' , error ) ;
80
-
79
+ } catch {
81
80
setQrCodeSvg ( null ) ;
82
81
setManualSetupKey ( null ) ;
83
82
}
@@ -88,6 +87,7 @@ export const useTwoFactorAuth = () => {
88
87
manualSetupKey,
89
88
recoveryCodesList,
90
89
hasSetupData,
90
+ errors,
91
91
clearSetupData,
92
92
fetchQrCode,
93
93
fetchSetupKey,
0 commit comments