Skip to content

Commit

Permalink
Frontend - redirect to login page when auth expired
Browse files Browse the repository at this point in the history
  • Loading branch information
lwih committed Feb 28, 2024
1 parent 9848674 commit 17d3f49
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions frontend/src/apollo-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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__) {
Expand All @@ -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,
]),
Expand Down

0 comments on commit 17d3f49

Please sign in to comment.