@@ -28,6 +28,11 @@ export const useTwoFactorAuth = () => {
2828 const [ qrCodeSvg , setQrCodeSvg ] = useState < string | null > ( null ) ;
2929 const [ manualSetupKey , setManualSetupKey ] = useState < string | null > ( null ) ;
3030 const [ recoveryCodesList , setRecoveryCodesList ] = useState < string [ ] > ( [ ] ) ;
31+ const [ errors , setErrors ] = useState < {
32+ qrCode ?: string ;
33+ setupKey ?: string ;
34+ recoveryCodes ?: string ;
35+ } > ( { } ) ;
3136
3237 const hasSetupData = useMemo < boolean > ( ( ) => qrCodeSvg !== null && manualSetupKey !== null , [ qrCodeSvg , manualSetupKey ] ) ;
3338
@@ -36,48 +41,47 @@ export const useTwoFactorAuth = () => {
3641 const { svg } = await fetchJson < TwoFactorSetupData > ( qrCode . url ( ) ) ;
3742
3843 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' } ) ) ;
4246 setQrCodeSvg ( null ) ;
4347 }
4448 } , [ ] ) ;
4549
4650 const fetchSetupKey = useCallback ( async ( ) : Promise < void > => {
4751 try {
4852 const { secretKey : key } = await fetchJson < TwoFactorSecretKey > ( secretKey . url ( ) ) ;
49-
5053 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' } ) ) ;
5456 setManualSetupKey ( null ) ;
5557 }
5658 } , [ ] ) ;
5759
60+ const clearErrors = useCallback ( ( ) : void => {
61+ setErrors ( { } ) ;
62+ } , [ ] ) ;
63+
5864 const clearSetupData = useCallback ( ( ) : void => {
5965 setManualSetupKey ( null ) ;
6066 setQrCodeSvg ( null ) ;
61- } , [ ] ) ;
67+ clearErrors ( ) ;
68+ } , [ clearErrors ] ) ;
6269
6370 const fetchRecoveryCodes = useCallback ( async ( ) : Promise < void > => {
6471 try {
6572 const codes = await fetchJson < string [ ] > ( recoveryCodes . url ( ) ) ;
6673
6774 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' } ) ) ;
7177 setRecoveryCodesList ( [ ] ) ;
7278 }
7379 } , [ ] ) ;
7480
7581 const fetchSetupData = useCallback ( async ( ) : Promise < void > => {
7682 try {
7783 await Promise . all ( [ fetchQrCode ( ) , fetchSetupKey ( ) ] ) ;
78- } catch ( error ) {
79- console . error ( 'Failed to fetch setup data:' , error ) ;
80-
84+ } catch {
8185 setQrCodeSvg ( null ) ;
8286 setManualSetupKey ( null ) ;
8387 }
@@ -88,6 +92,8 @@ export const useTwoFactorAuth = () => {
8892 manualSetupKey,
8993 recoveryCodesList,
9094 hasSetupData,
95+ errors,
96+ clearErrors,
9197 clearSetupData,
9298 fetchQrCode,
9399 fetchSetupKey,
0 commit comments