@@ -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,9 +41,8 @@ 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
} , [ ] ) ;
@@ -48,36 +52,37 @@ export const useTwoFactorAuth = () => {
48
52
const { secretKey : key } = await fetchJson < TwoFactorSecretKey > ( secretKey . url ( ) ) ;
49
53
50
54
setManualSetupKey ( key ) ;
51
- } catch ( error ) {
52
- console . error ( 'Failed to fetch setup key:' , error ) ;
53
-
55
+ } catch {
56
+ setErrors ( ( prev ) => ( { ...prev , setupKey : 'Failed to fetch a setup key' } ) ) ;
54
57
setManualSetupKey ( null ) ;
55
58
}
56
59
} , [ ] ) ;
57
60
61
+ const clearErrors = useCallback ( ( ) : void => {
62
+ setErrors ( { } ) ;
63
+ } , [ ] ) ;
64
+
58
65
const clearSetupData = useCallback ( ( ) : void => {
59
66
setManualSetupKey ( null ) ;
60
67
setQrCodeSvg ( null ) ;
61
- } , [ ] ) ;
68
+ clearErrors ( ) ;
69
+ } , [ clearErrors ] ) ;
62
70
63
71
const fetchRecoveryCodes = useCallback ( async ( ) : Promise < void > => {
64
72
try {
65
73
const codes = await fetchJson < string [ ] > ( recoveryCodes . url ( ) ) ;
66
74
67
75
setRecoveryCodesList ( codes ) ;
68
- } catch ( error ) {
69
- console . error ( 'Failed to fetch recovery codes:' , error ) ;
70
-
76
+ } catch {
77
+ setErrors ( ( prev ) => ( { ...prev , recoveryCodes : 'Failed to fetch recovery codes' } ) ) ;
71
78
setRecoveryCodesList ( [ ] ) ;
72
79
}
73
80
} , [ ] ) ;
74
81
75
82
const fetchSetupData = useCallback ( async ( ) : Promise < void > => {
76
83
try {
77
84
await Promise . all ( [ fetchQrCode ( ) , fetchSetupKey ( ) ] ) ;
78
- } catch ( error ) {
79
- console . error ( 'Failed to fetch setup data:' , error ) ;
80
-
85
+ } catch {
81
86
setQrCodeSvg ( null ) ;
82
87
setManualSetupKey ( null ) ;
83
88
}
@@ -88,6 +93,8 @@ export const useTwoFactorAuth = () => {
88
93
manualSetupKey,
89
94
recoveryCodesList,
90
95
hasSetupData,
96
+ errors,
97
+ clearErrors,
91
98
clearSetupData,
92
99
fetchQrCode,
93
100
fetchSetupKey,
0 commit comments