@@ -27,6 +27,7 @@ import {
27
27
import { coderClientApiRef } from '../../api/CoderClient' ;
28
28
import { CoderLogo } from '../CoderLogo' ;
29
29
import { CoderAuthFormDialog } from '../CoderAuthFormDialog' ;
30
+ import { AxiosError } from 'axios' ;
30
31
31
32
const BACKSTAGE_APP_ROOT_ID = '#root' ;
32
33
const FALLBACK_UI_OVERRIDE_CLASS_NAME = 'backstage-root-override' ;
@@ -143,30 +144,30 @@ function useAuthState(): CoderAuth {
143
144
// outside React because we let the user connect their own queryClient
144
145
const queryClient = useQueryClient ( ) ;
145
146
useEffect ( ( ) => {
146
- // Pseudo-mutex; makes sure that if we get a bunch of errors, only one
147
- // revalidation will be processed at a time
148
- let isRevalidating = false ;
149
-
150
- const revalidateTokenOnError = async ( event : QueryCacheNotifyEvent ) => {
151
- const queryError = event . query . state . error ;
147
+ if ( ! isAuthenticated ) {
148
+ return undefined ;
149
+ }
152
150
153
- const shouldRevalidate =
154
- isAuthenticated &&
155
- ! isRevalidating &&
156
- BackstageHttpError . isInstance ( queryError ) &&
157
- queryError . status === 401 ;
151
+ const onCoderQueryError = ( event : QueryCacheNotifyEvent ) => {
152
+ const queryKey = event . query . queryKey ;
153
+ const isCoderQuery =
154
+ Array . isArray ( queryKey ) && queryKey [ 0 ] === CODER_QUERY_KEY_PREFIX ;
158
155
159
- if ( ! shouldRevalidate ) {
156
+ if ( ! isCoderQuery ) {
160
157
return ;
161
158
}
162
159
163
- isRevalidating = true ;
164
- await queryClient . refetchQueries ( { queryKey : sharedAuthQueryKey } ) ;
165
- isRevalidating = false ;
160
+ const queryError = event . query . state . error ;
161
+ const failedBecauseOfInvalidAuth =
162
+ queryError instanceof AxiosError && queryError . status === 401 ;
163
+
164
+ if ( failedBecauseOfInvalidAuth ) {
165
+ queryClient . setQueryData ( sharedAuthQueryKey , false ) ;
166
+ }
166
167
} ;
167
168
168
169
const queryCache = queryClient . getQueryCache ( ) ;
169
- const unsubscribe = queryCache . subscribe ( revalidateTokenOnError ) ;
170
+ const unsubscribe = queryCache . subscribe ( onCoderQueryError ) ;
170
171
return unsubscribe ;
171
172
} , [ queryClient , isAuthenticated ] ) ;
172
173
0 commit comments