I am putting together a simple website but found that the browsers refuse to save any cookies my server sends them. Under what conditions might a browser reject the Set-Cookie
header from a server? The cookies are fairly ordinary:
Set-Cookie: auth=XXXXX; expires=Wed, 26 Jul 2023 14:47:20 GMT; HttpOnly; Max-Age=300; Path=/; SameSite=Lax
but I also tried sending quite a few variants of simple cookies (e.g. no HttpOnly, with a nd without expire, etc).
Set-Cookie: COOKIE-NAME=COOKIE-VALUE; Path=/
Yet nothing seems to work. When inspecting the response headers they look as you would expect, and Chrome' dev tools do not indicate any issue with e.g. SAMESITE settings.
FE and BE are cross-domain - which seems like it may be problematic.
Frontend: localhost:3000
backend : localhost:8000
I have tried Chrome, Firefox, and Safari but all three behave the same way. I have a more technical question here but I think this question is fundamentally the issue without Axios/Django specifics.
I think it may have something to do with requests originating from javascript in the frontend versus requests made by the browser.
I was able to set a cookie (and the browser saved it) when the backend served some HTML directly (as opposed to being an API server, which is what I am having issues with). However, when I try to use Axios/fetch within Javascript (React) the browser ignores Set-Cookie
. Perhaps it is CORS related?
Chrome dev tools shows the request type that does work (the direct HTML/text served from backend) as document
, while the request from Javascript (when backend is just an API is xhr
).