apollo-client: Apollo Client does not pass cookies
I am currently using nextJS with apollo and it’s completely unusable for me because the cookie is not passing in every request.
I would be much grateful if someone can just point me to right direction to pass cookies properly.
Even the apollo nextjs example is buggy itself https://github.com/adamsoffer/next-apollo-example
Even in that example, cookies are not being sent in the request.
I am trying every possible way of setting cookies in config without success.
Some people say swapping ApolloClient to ApolloBoost have solved it but neither of the packages work for me.
Below is an example of what I have tried
new ApolloClient({
connectToDevTools: process.browser,
ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once)
link: new HttpLink({
uri: APOLLO_ENDPOINT, // Server URL (must be absolute)
opts:{
credentials:'include'
},
credentials: 'include', // Additional fetch() options like `credentials` or `headers`,
}),
cache: new InMemoryCache().restore(initialState || {}),
fetchOptions:{
credentials:'include'
},
credentials:'include'
})
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 44
- Comments: 93 (5 by maintainers)
Commits related to this issue
- Allow docker system prune, fix apollo client headers issue? https://github.com/apollographql/apollo-client/issues/4190 — committed to sophiabrandt/nextjs-ecommerce by sophiabrandt 3 years ago
Figured it out.
credentials:'include'
should be in the root of the config. Like,I was using inside of
fetchOptions
…Put the
credentials: "include"
in your httpLink, like so.Then put that in your ApolloClient. Worked finally without a bunch of custom stuff.
I’m pretty sure I’m still having this issue. My rest calls have cookies attached, however my graphql queries don’t.
I have the same issue with apolloboost the cookies not sent with the request
Hello everybody from 2021, and… I’m having the same issue ((
Add cookie to getServerSideProps on a page by page basis
Easy quick method to add cookie is to pass context.
If client side is not storing cookie on login or whatever
adding credentials property seems to do the trick
The docs are here: https://www.apollographql.com/docs/react/networking/authentication/#cookie
i’m using
next-with-apollo
and it’s working, you can do something like thisTook us a while to figure this out, but here were our couple of sticking points.
Currently the only place we configured
credentials: 'include'
was in the HttpLink configuration object. This will fail giving you a cors error if you have not already set them up.Assuming your setup is similar to ours, you are using the
applyMiddleware
function to make changes to an instance of an express server. Within this object is where you must pass your cors configuration.Ours looks like this for example (for dev):
All of a sudden our client was passing cookies. Good luck!
Hi, I found that when I want to make request with “Cookie” then apollo client is not sending it, but just when I change it to “Cookies” then everything is ok XD
Hi there,
I’m still having the issue here. I’m working with:
withCredentials is sets to true in my apollo.config.ts file:
Is there anybody that can tell us why ?
Heyo! I was running into the issue of apollo-client sending / setting cookies client side fine, but was getting problems with it server side. I use an afterware server-side to set cookies. Here’s my fix with typescript:
I’ve fixed it for myself using the latest libraries next@9.0.3, next-with-apollo@4.2.0 and react-apollo@3.0.0. Cookies are passed with every request, SSR working as expected and no errors. Code is here. I had to remove
.restore(initialState || {})
so it’s justcache: new InMemoryCache()
and now it’s fully working. Only thing still not working is Safari. Anyone fixed it for Safari?This also fixed my issue, the docs are a bit confusing as credentials: ‘include’ is nested under fetchOptions; putting in the root fixed my issue. Is there a reason why this option can be put in two places? Only the root worked for me and caused a lot of confusion
@rrakso Did you mean like this?
I’m having a similar problem, but I actually think it is a browser issue and nothing to do with the Apollo Client.
When I have my front-end hosted on Heroku like
frontend.herokuapp.com
and my yoga backend on something likebackend.herokupapp.com
, my Graphql queries will only retain the cookie if my browsers do NOT have “Disable 3rd Party Cookies” set or Safari’s “Prevent Cross-site Tracking” enable.It seems to me, the browser considers any cookie from different subdomain’s to be 3rd party.
There are varying degrees of this:
Opera - Cookies blocked if “Block Third Party Cookies” enabled Firefox - Cookies work even with Block Third Party Cookies - Trackers" enabled but blocked if “All third party cookies” is selected Chrome - Cookies work unless “Block Third Party Cookies” enabled
@neil-gebbie-smarterley - I have an authToken saved in memory that expires every 10mn that I pass on each and every requests. And a long-lived refreshToken that I must NOT pass on every requests saved in an httpOnly cookie. This refresh token is used every 10mn to get a new authToken and a new refreshToken.
Thanks I’ll take a look at it.
Hi there, Thanks for your precious infos.
We want to store refreshTokens only (not authToken) in an httpOnly cookie but if we use credentials:include, the refresh token will be passed on every request which is far from optimal. Is my use case unique or am I missing something?
Thanks
Thank you everyone. I found solution for myself I’ve decided to use next-apollo-example that I found on the internet with a little tweaks.
If someone needs here is the code
initApollo.js
witApollo.js
_app.js
And added this middleware code to the backend
@mdashmiller thanks. It worked for me as well. 😃
After doing this, the session created on back-end via
express-session
now appears in Application > Cookies 😍I had this issue using apollo client and apollo-server-express
With my use-case I needed to put credentials: ‘include’ in two places:
First, the cookie was not being created. Adding credentials: ‘include’ to createHTTPLink() solved this. Then I was getting connection refused errors when I would query the server. Placing credentials: ‘include’ in new ApolloClient() solved this issue.
I think I understand what’s happening. The client and server I’m working with are both subdomains of herokuapp (e.g…,
ui.herokuapp.com
andweb-server.herokuapp.com
). From https://devcenter.heroku.com/articles/cookies-and-herokuapp-com:Using a custom domain will probably resolve the issue.
I had to do this for our build a few days ago, here is how I did it - https://gist.github.com/neil-gebbie-smarterley/cd8356df4c786c4c9dacfc9d46e890ac
Our set it is: Next/Apollo Client/Apollo Server/REST data source
It’s basically just passing it through to the server, I’m not sure if the document.cookie is needed for subsequent requests, but it seems to be working ok for us, not in production yet, but it’s passing cookies.
@dclipca FYI I think that because you’re using a custom link, the second
credentials: "include"
(the one you’re passing directly to ApplloClient) is not needed.@gregg-cbs I believe this is what we want to avoid (although this is what I do now). Repetitive code in many pages is a bad practice. Need to find a solution for this.
@allanesquina
@apollo/client@3.0.2
is a fairly old version of AC3; can you try with@apollo/client@latest
? If that doesn’t work, can you provide a small runnable reproduction that clearly demonstrates the issue you’re seeing? The link you provided is for one of Next’s demo apps. To be able to properly troubleshoot this, we’ll need a focused reproduction that shows the problem. Thanks!I’m using this example, you may use that to reproduce as it has the same issue.
https://github.com/vercel/next.js/tree/canary/examples/api-routes-apollo-server-and-client-auth
Try to use
getServerSideProps
to get data from a protected resolver.I hope it helps.
@alex-r89 I think you should also apply the same cors options to
.applyMiddleware
. You can try also cors settings inapp.listen
. At least that’s how it’s working for meYou should also try to set the domain value in the cookie object because it might be set with your server domain
api.mydomain.xyz
and not.mydomain.xyz
(check in devtools with what domain the cookie is created, I’m not sure right now)As both @lawwantsin and @chemicalkosek stated, to ensure that my cookie is boh not wiped out and detected on refresh, I did the following:
I think you need to explicitly pass the cookies through
getInitialProps
in your withApolloClient HOC. Check out the gist(https://gist.github.com/neil-gebbie-smarterley/cd8356df4c786c4c9dacfc9d46e890ac) I posted - it might look like a lot of work to get it going, but if you log the cookies out at every step of the process you can see them being passed along from the browser through Apollo, and then onto it’s final destination.We have this set up working in production.