@@ -13,52 +13,67 @@ const fetchJson = async <T>(url: string): Promise<T> => {
1313 return response . json ( ) ;
1414} ;
1515
16- const qrCodeSvg = ref < string | null > ( null ) ;
16+ const errors = ref < string [ ] > ( [ ] ) ;
1717const manualSetupKey = ref < string | null > ( null ) ;
18+ const qrCodeSvg = ref < string | null > ( null ) ;
1819const recoveryCodesList = ref < string [ ] > ( [ ] ) ;
1920
2021const hasSetupData = computed < boolean > ( ( ) => qrCodeSvg . value !== null && manualSetupKey . value !== null ) ;
2122
2223export const useTwoFactorAuth = ( ) => {
2324 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 ( ) ) ;
2527
26- qrCodeSvg . value = svg ;
28+ qrCodeSvg . value = svg ;
29+ } catch {
30+ errors . value . push ( 'Failed to fetch QR code' ) ;
31+ qrCodeSvg . value = null ;
32+ }
2733 } ;
2834
2935 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 ( ) ) ;
3138
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+ }
3344 } ;
3445
3546 const clearSetupData = ( ) : void => {
3647 manualSetupKey . value = null ;
3748 qrCodeSvg . value = null ;
49+ clearErrors ( ) ;
50+ } ;
51+
52+ const clearErrors = ( ) : void => {
53+ errors . value = [ ] ;
3854 } ;
3955
4056 const clearTwoFactorAuthData = ( ) : void => {
4157 clearSetupData ( ) ;
42-
58+ clearErrors ( ) ;
4359 recoveryCodesList . value = [ ] ;
4460 } ;
4561
4662 const fetchRecoveryCodes = async ( ) : Promise < void > => {
4763 try {
64+ clearErrors ( ) ;
4865 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' ) ;
5268 recoveryCodesList . value = [ ] ;
5369 }
5470 } ;
5571
5672 const fetchSetupData = async ( ) : Promise < void > => {
5773 try {
74+ clearErrors ( ) ;
5875 await Promise . all ( [ fetchQrCode ( ) , fetchSetupKey ( ) ] ) ;
59- } catch ( error ) {
60- console . error ( 'Failed to fetch setup data:' , error ) ;
61-
76+ } catch {
6277 qrCodeSvg . value = null ;
6378 manualSetupKey . value = null ;
6479 }
@@ -68,8 +83,10 @@ export const useTwoFactorAuth = () => {
6883 qrCodeSvg,
6984 manualSetupKey,
7085 recoveryCodesList,
86+ errors,
7187 hasSetupData,
7288 clearSetupData,
89+ clearErrors,
7390 clearTwoFactorAuthData,
7491 fetchQrCode,
7592 fetchSetupKey,
0 commit comments