In a server I'm building I want to set a cookie with an auth token that gets set when the user logs in. The login request is a POST XHR with a form containing my username and password. I'm testing this on http://localhost:3000.
I can see that the server is trying to set the cookie because the response has this header:
set-cookie: token=json-web-token-value;Expires=Fri, 12 Mar 2021 18:19:12 GMT
However Chrome seems to be ignoring this. No subsequent requests to the API contain the cookie and it also isn't in the list of cookies for http://localhost:3000 in the Application -> Cookies devtools section.
When I try to set the cookie with JS manually like
document.cookie = 'token=json-web-token-value;Expires=Fri, 12 Mar 2021 18:19:12 GMT'
then the cookie does get set correctly. So the problem clearly isn't with the contents of the cookie itself.
What am I doing wrong here?
EDIT: Firefox
I decided to check whether Firefox works any differently, and lo and behold the cookie does get set. The only thing that seemed odd is that the path was set to /auth – possibly because the login endpoint path is /auth/login. Setting the path in my server to / manually results in a cookie of
token=json-web-token-value;Path=/;Expires=Fri, 12 Mar 2021 18:34:27 GMT
which now seems to work even in Chrome!
So now my questions are:
- Why does leaving the path undefined make Chrome completely ignore the cookie?
- Why does not specifying a path make Firefox set the path to
/authand not/, or/auth/loginfor that matter?