Hello everyone!
I need some help with how I authenticate apollo client queries to a 3rd party graphql server.
Right now, I have two cookies AuthToken and RefreshToken. AuthToken only lasts for 60 seconds, but I can get a new one with the given RefreshToken. My problem right now is that I set the Bearer token inside the ApolloClient. And in that object, I do not have access to doing queries to third parties, so I have no way to getting a new AuthToken. Does anyone have a better way to do this?
const httpLink = new HttpLink({
uri: 'https://graphql.SERVER',
});
const authMiddleware = new ApolloLink((operation, forward) => {
let AuthToken = Cookies.get('authToken');
console.log('AuthToken', AuthToken);
if (AuthToken) {
operation.setContext(({ headers = {} }) => ({
headers: {
...headers,
authorization: `Bearer ${AuthToken}`,
},
}));
} else {
operation.setContext(({ headers = {} }) => ({
headers: {
...headers,
},
}));
}
return forward(operation);
});
const link = createHttpLink({
uri: `https://graphql.SERVER`,
});
const client = new ApolloClient({
connectToDevTools: true,
link: concat(authMiddleware, httpLink),
cache,
credentials: 'include',
});
export default client;