fix: :lock: Unauthenticate user on 401 gql response
Tim Izzo tim@octree.ch
Wed, 31 Aug 2022 09:07:08 +0000
1 files changed,
6 insertions(+),
8 deletions(-)
jump to
M
frontend/lib/apolloClient.ts
→
frontend/lib/apolloClient.ts
@@ -29,15 +29,13 @@ },
}; }); -const errorLink = onError(({graphQLErrors = []}) => { - const [error] = graphQLErrors; - console.error({graphQLErrors}); +const errorLink = onError(error => { + const {networkError, graphQLErrors} = error; + console.error({networkError, graphQLErrors}); + const isUnauthorized = networkError?.response?.status === 401; - if ( - error?.message === 'Invalid token.' || - error?.message === 'User Not Found' || - error?.message === 'Your account has been blocked by the administrator.' - ) { + if (isUnauthorized) { + console.error('Unauthorized response received from GraphQL. Logout user.'); useAuthStore.getState().setToken(); useAuthStore.getState().setUser(); localStorage.removeItem('token');