Authentication lost

Ok! So we’re going to do the “quick and dirty” way first - and if this works for you - then we’ll add it into Redwood.

The way to do this with Apollo is to instantiate a new client, and to pass it a link option (docs):

import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/client';
import { onError } from '@apollo/client/link/error';

import { logout } from './logout';

const httpLink = new HttpLink({ uri: '/graphql' });

const logoutLink = onError(({ networkError }) => {
  if (networkError.statusCode === 401) logout();
})

const client = new ApolloClient({
  cache: new InMemoryCache(),
  link: logoutLink.concat(httpLink),
});

But, for Redwood the integration point should be to supply a custom graphql configuration:

// src/index.js


const httpLink = new HttpLink({ uri: '/graphql' });

const logoutLink = onError(({ networkError }) => {
  if (networkError.statusCode === 401) navigate('/login?status=timeout')
})

const config = {
  cache: new InMemoryCache(), // I'm not sure if this is required.
  link: logoutLink.concat(httpLink),
}

return (
<RedwoodProvider graphQLClientConfig={config}>
{...}
</RedwoodProvider>
)

I would call 'logOuton the/login` page to reset the state. Please let me know how this goes, it’s super late and I don’t know if I’ve parsed this code in my brain correctly.

1 Like