diff --git a/frontend/src/apollo-client.ts b/frontend/src/apollo-client.ts index 623e5198..09bdf00e 100644 --- a/frontend/src/apollo-client.ts +++ b/frontend/src/apollo-client.ts @@ -3,7 +3,9 @@ import { loadDevMessages, loadErrorMessages } from '@apollo/client/dev' import AuthToken from './auth/token' import { setContext } from '@apollo/client/link/context' import { RetryLink } from "@apollo/client/link/retry"; +import { ErrorResponse, onError } from "@apollo/client/link/error"; +const authToken = new AuthToken() if (true) { // if (__DEV__) { @@ -30,12 +32,24 @@ const authLink = setContext((_, {headers}) => { } }) +const errorLink = onError(({graphQLErrors, networkError, operation, forward}: ErrorResponse) => { + const {response} = operation.getContext() + + if ([401, 403].indexOf(response.status) !== -1) { + authToken.remove() + window.location.replace('/login') + } + + return forward(operation); +}); + export const apolloCache = new InMemoryCache({}) const client = new ApolloClient({ cache: apolloCache, link: ApolloLink.from([ authLink, + errorLink, retryLink, httpLink, ]),