@@ -22,9 +22,11 @@ import {
22
22
type ConfirmEmailResponse ,
23
23
Roles ,
24
24
Products ,
25
+ AuthorizationError ,
25
26
} from "utils/atlas" ;
26
27
27
28
import { isUndefined } from "src/utils" ;
29
+ import { GraphQLError } from "graphql" ;
28
30
29
31
interface IAtlasProvider {
30
32
isVerified : boolean ;
@@ -94,16 +96,32 @@ const AtlasProvider: React.FC<{ children?: React.ReactNode }> = ({ children }) =
94
96
} , [ authToken , address ] ) ;
95
97
96
98
useEffect ( ( ) => {
97
- // initial verfiy check
98
- setIsVerified ( verifySession ( ) ) ;
99
+ let timeoutId : NodeJS . Timeout ;
99
100
100
- // verify session every 5 sec
101
- const intervalId = setInterval ( ( ) => {
102
- setIsVerified ( verifySession ( ) ) ;
103
- } , 5000 ) ;
101
+ const verifyAndSchedule = ( ) => {
102
+ console . log ( "checking" ) ;
103
+
104
+ // initial verfiy check
105
+ const isValid = verifySession ( ) ;
106
+ setIsVerified ( isValid ) ;
107
+
108
+ if ( isValid && authToken ) {
109
+ try {
110
+ const payload = decodeJwt ( authToken ) ;
111
+ const expiresIn = ( payload . exp as number ) * 1000 - Date . now ( ) ;
112
+
113
+ timeoutId = setTimeout ( verifyAndSchedule , Math . max ( 0 , expiresIn ) ) ;
114
+ } catch ( err ) {
115
+ console . error ( "Error decoding JWT:" , err ) ;
116
+ setIsVerified ( false ) ;
117
+ }
118
+ }
119
+ } ;
120
+
121
+ verifyAndSchedule ( ) ;
104
122
105
123
return ( ) => {
106
- clearInterval ( intervalId ) ;
124
+ clearTimeout ( timeoutId ) ;
107
125
} ;
108
126
} , [ authToken , verifySession , address ] ) ;
109
127
@@ -140,6 +158,21 @@ const AtlasProvider: React.FC<{ children?: React.ReactNode }> = ({ children }) =
140
158
return ! isUndefined ( user . email ) ;
141
159
} , [ user ] ) ;
142
160
161
+ async function fetchWithAuthErrorHandling < T > ( request : ( ) => Promise < T > ) : Promise < T > {
162
+ try {
163
+ return await request ( ) ;
164
+ } catch ( error ) {
165
+ if (
166
+ error instanceof AuthorizationError ||
167
+ ( error instanceof GraphQLError && error . extensions [ "code" ] === "UNAUTHENTICATED" )
168
+ ) {
169
+ setIsVerified ( false ) ;
170
+ throw error ;
171
+ }
172
+ throw error ;
173
+ }
174
+ }
175
+
143
176
/**
144
177
* @description authorise user and enable authorised calls
145
178
*/
@@ -173,7 +206,7 @@ const AtlasProvider: React.FC<{ children?: React.ReactNode }> = ({ children }) =
173
206
if ( ! address || ! isVerified ) return false ;
174
207
setIsAddingUser ( true ) ;
175
208
176
- const userAdded = await addUserToAtlas ( atlasGqlClient , userSettings ) ;
209
+ const userAdded = await fetchWithAuthErrorHandling ( ( ) => addUserToAtlas ( atlasGqlClient , userSettings ) ) ;
177
210
refetchUser ( ) ;
178
211
179
212
return userAdded ;
@@ -199,7 +232,8 @@ const AtlasProvider: React.FC<{ children?: React.ReactNode }> = ({ children }) =
199
232
if ( ! address || ! isVerified ) return false ;
200
233
setIsUpdatingUser ( true ) ;
201
234
202
- const emailUpdated = await updateEmailInAtlas ( atlasGqlClient , userSettings ) ;
235
+ // const emailUpdated = await updateEmailInAtlas(atlasGqlClient, userSettings);
236
+ const emailUpdated = await fetchWithAuthErrorHandling ( ( ) => updateEmailInAtlas ( atlasGqlClient , userSettings ) ) ;
203
237
refetchUser ( ) ;
204
238
205
239
return emailUpdated ;
@@ -227,9 +261,8 @@ const AtlasProvider: React.FC<{ children?: React.ReactNode }> = ({ children }) =
227
261
if ( ! address || ! isVerified || ! atlasUri || ! authToken ) return null ;
228
262
setIsUploadingFile ( true ) ;
229
263
230
- const hash = await uploadToIpfs (
231
- { baseUrl : atlasUri , authToken } ,
232
- { file, name : file . name , role, product : Products . CourtV2 }
264
+ const hash = await fetchWithAuthErrorHandling ( ( ) =>
265
+ uploadToIpfs ( { baseUrl : atlasUri , authToken } , { file, name : file . name , role, product : Products . CourtV2 } )
233
266
) ;
234
267
return hash ? `/ipfs/${ hash } ` : null ;
235
268
} catch ( err : any ) {
0 commit comments