@@ -93,63 +93,63 @@ export const AuthGuard: React.FC<
9393
9494  useEffect ( ( )  =>  { 
9595    async  function  getAuth ( )  { 
96-       await  navigator . locks . request ( 
97-         `lock_authGuard_loader` , 
98-         {  mode : 'exclusive'  } , 
99-         async  ( lock )  =>  { 
100-           try  { 
101-             if  ( ! authCheckRoute )  { 
102-               setIsAuthenticated ( true ) ; 
103-               return ; 
104-             } 
105-             if  ( validRoles . length  ===  0 )  { 
106-               setIsAuthenticated ( true ) ; 
107-               return ; 
108-             } 
109- 
110-             // Check for cached response first 
111-             setIsLoading ( true ) ; 
112-             const  cachedData  =  await  getCachedResponse ( service ,  authCheckRoute ) ; 
113-             if  ( cachedData  !==  null )  { 
114-               const  userRoles  =  cachedData . data . roles ; 
115-               let  authenticated  =  false ; 
116-               for  ( const  item  of  userRoles )  { 
117-                 if  ( validRoles . indexOf ( item )  !==  - 1 )  { 
118-                   authenticated  =  true ; 
119-                   break ; 
120-                 } 
121-               } 
122-               setUsername ( cachedData . data . username ) ; 
123-               setRoles ( cachedData . data . roles ) ; 
124-               setIsAuthenticated ( authenticated ) ; 
125-               setIsLoading ( false ) ; 
126-               return ; 
127-             } 
128- 
129-             // If no cache, make the API call 
130-             const  result  =  await  api . get ( authCheckRoute ) ; 
131-             // Cache just the response data 
132-             await  setCachedResponse ( service ,  authCheckRoute ,  result . data ) ; 
133- 
134-             const  userRoles  =  result . data . roles ; 
96+       if  ( ! authCheckRoute )  { 
97+         setIsAuthenticated ( true ) ; 
98+         return ; 
99+       } 
100+       if  ( validRoles . length  ===  0 )  { 
101+         setIsAuthenticated ( true ) ; 
102+         return ; 
103+       } 
104+       const  cachedData  =  await  getCachedResponse ( service ,  authCheckRoute ) ; 
105+       const  lockMode  =  cachedData  ? 'shared'  : 'exclusive' ; 
106+       await  navigator . locks . request ( `lock_authGuard_loader` ,  {  mode : lockMode  } ,  async  ( lock )  =>  { 
107+         try  { 
108+           // We have to check the cache twice because if one exclusive process before us 
109+           // retrieved it we should now be able to use it. Theoretically this shouldn't 
110+           // ever trigger because AuthGuard on the navbar will always call first, but 
111+           // to protect against future implementations. 
112+           setIsLoading ( true ) ; 
113+           const  cachedData  =  await  getCachedResponse ( service ,  authCheckRoute ) ; 
114+           if  ( cachedData  !==  null )  { 
115+             const  userRoles  =  cachedData . data . roles ; 
135116            let  authenticated  =  false ; 
136117            for  ( const  item  of  userRoles )  { 
137118              if  ( validRoles . indexOf ( item )  !==  - 1 )  { 
138119                authenticated  =  true ; 
139120                break ; 
140121              } 
141122            } 
123+             setUsername ( cachedData . data . username ) ; 
124+             setRoles ( cachedData . data . roles ) ; 
142125            setIsAuthenticated ( authenticated ) ; 
143-             setRoles ( result . data . roles ) ; 
144-             setUsername ( result . data . username ) ; 
145126            setIsLoading ( false ) ; 
146-           }  catch  ( e )  { 
147-             setIsAuthenticated ( false ) ; 
148-             setIsLoading ( false ) ; 
149-             console . error ( e ) ; 
127+             return ; 
150128          } 
129+ 
130+           // If no cache, make the API call 
131+           const  result  =  await  api . get ( authCheckRoute ) ; 
132+           // Cache just the response data 
133+           await  setCachedResponse ( service ,  authCheckRoute ,  result . data ) ; 
134+ 
135+           const  userRoles  =  result . data . roles ; 
136+           let  authenticated  =  false ; 
137+           for  ( const  item  of  userRoles )  { 
138+             if  ( validRoles . indexOf ( item )  !==  - 1 )  { 
139+               authenticated  =  true ; 
140+               break ; 
141+             } 
142+           } 
143+           setIsAuthenticated ( authenticated ) ; 
144+           setRoles ( result . data . roles ) ; 
145+           setUsername ( result . data . username ) ; 
146+           setIsLoading ( false ) ; 
147+         }  catch  ( e )  { 
148+           setIsAuthenticated ( false ) ; 
149+           setIsLoading ( false ) ; 
150+           console . error ( e ) ; 
151151        } 
152-       ) ; 
152+       } ) ; 
153153    } 
154154    getAuth ( ) ; 
155155  } ,  [ baseEndpoint ,  authCheckRoute ,  service ] ) ; 
0 commit comments