fix unauthenticated (#2203)

Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
Tom Avalexing
2023-10-24 09:54:45 +03:00
committed by GitHub
parent 2b590a1c33
commit 80d558559f

View File

@ -3,6 +3,7 @@ import {
ApolloClient, ApolloClient,
ApolloClientOptions, ApolloClientOptions,
ApolloLink, ApolloLink,
fromPromise,
ServerError, ServerError,
ServerParseError, ServerParseError,
} from '@apollo/client'; } from '@apollo/client';
@ -22,8 +23,6 @@ import { loggerLink } from '../utils';
const logger = loggerLink(() => 'Twenty'); const logger = loggerLink(() => 'Twenty');
let isRefreshing = false;
export interface Options<TCacheShape> extends ApolloClientOptions<TCacheShape> { export interface Options<TCacheShape> extends ApolloClientOptions<TCacheShape> {
onError?: (err: GraphQLErrors | undefined) => void; onError?: (err: GraphQLErrors | undefined) => void;
onNetworkError?: (err: Error | ServerParseError | ServerError) => void; onNetworkError?: (err: Error | ServerParseError | ServerError) => void;
@ -78,7 +77,6 @@ export class ApolloFactory<TCacheShape> implements ApolloManager<TCacheShape> {
retryIf: (error) => !!error, retryIf: (error) => !!error,
}, },
}); });
const errorLink = onError( const errorLink = onError(
({ graphQLErrors, networkError, forward, operation }) => { ({ graphQLErrors, networkError, forward, operation }) => {
if (graphQLErrors) { if (graphQLErrors) {
@ -87,22 +85,15 @@ export class ApolloFactory<TCacheShape> implements ApolloManager<TCacheShape> {
for (const graphQLError of graphQLErrors) { for (const graphQLError of graphQLErrors) {
switch (graphQLError?.extensions?.code) { switch (graphQLError?.extensions?.code) {
case 'UNAUTHENTICATED': { case 'UNAUTHENTICATED': {
if (!isRefreshing) { return fromPromise(
isRefreshing = true;
renewToken(uri, this.tokenPair) renewToken(uri, this.tokenPair)
.then((tokens) => { .then((tokens) => {
onTokenPairChange?.(tokens); onTokenPairChange?.(tokens);
return true;
}) })
.catch(() => { .catch(() => {
onUnauthenticatedError?.(); onUnauthenticatedError?.();
return false; }),
}) ).flatMap(() => forward(operation));
.finally(() => {
isRefreshing = false;
});
}
return forward(operation);
} }
default: default:
if (isDebugMode) { if (isDebugMode) {