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