0

I am using Apollo Client in my React app.

When a user is logged-in in our cms we set a HttpOnly cookie. I can see in my React app (browser) when I inspect with devtools this cookie has been set: MYNAMEffb4afd023baac83115bf7882.

I need to send a custom Request header to each GraphQl query request. But only when above cookie has been set.

The custom header:

X-My-Name: Name

What are the steps I have to follow?

meez
  • 3,783
  • 5
  • 37
  • 91
  • This can be done in a [link](https://www.apollographql.com/docs/react/api/link/introduction/) – Michel Floyd Oct 29 '22 at 17:36
  • @MichelFloyd thanks! But how do I check in React if `HttpOnly` cookie has been set? So in the custom link I can add an if statement `if (hasMyHttpOnlyCookie) {// link code}`. As far as I know you can't read an `HttpOnly` cookie in your frontend? Otherwise it will send that request header all the time in every GraphQL request. Also for 'normal' users? – meez Oct 30 '22 at 07:25
  • https://stackoverflow.com/a/63877877/2805154 – Michel Floyd Oct 30 '22 at 19:07
  • @MichelFloyd thanks again. So if I am not mistaken, the way to go is setting another non-httponly cookie as explained [here](https://stackoverflow.com/a/63877877/4822666). Is that correct? – meez Oct 30 '22 at 21:16
  • That's one way. Or you could avoid cookies altogether and use custom headers and local storage. – Michel Floyd Oct 30 '22 at 23:17
  • @MichelFloyd ok but how should I use custom headers and local storage in this case. What will be the steps to get this working with local storage variant? – meez Oct 31 '22 at 08:19
  • @MichelFloyd is this also possible with JWT token? – meez Oct 31 '22 at 10:31
  • Yes, you can use JWT with custom headers. – Michel Floyd Oct 31 '22 at 16:19
  • @MichelFloyd thanks I am trying it already. I raised [this question](https://stackoverflow.com/questions/74264345/make-a-condition-based-on-request-url-status-code-and-response-header-value-in-r). Do you know? – meez Oct 31 '22 at 18:33

0 Answers0