12

I'm using NodeJS with the Express framework with the cookie-parser middleware on localhost:3333. My frontend is served by react dev server on localhost:3000. This is my first attempt at anything to do with cookies. Here are the options i set for my cookie: { expires: maxAge , httpOnly: true, sameSite: 'none', secure: true, domain: null }. I've also tried with and without the sameSite, httpOnly and secure options. As well as domain=localhost.

Here is a header from the http response i get when trying to set a cookie:

Set-Cookie:

sessionid=119faa6a3fcfab0628998ff2592e04ceb06cda12; Path=/; Expires=Sun, 29 Jan 2023 15:20:16 GMT; HttpOnly; Secure; SameSite=None

But the problem is the cookie does NOT show up in Application/Cookies in Chrome or Storage/Cookies in Firefox. I also can't access the cookie as a request cookie on different endpoints. It does however work on my vps. The vps uses a proper domain name as well as a proxy so that all requests appear to come from the same domain/port, i don't know if that makes a difference or not.

I've also tried adding 127.0.0.1 dev.mylocalhost.com to /etc/hosts in order to fake a domain name on localhost. Just read something about this, not quite sure how that is supposed to work. I don't own this domain, this is just in my local /etc/hosts file. I have not tried using a proxy on localhost, so the cookie is being set from the server on port 3333 while the client is on port 3000. Again not sure if that makes a difference.

1 Answers1

0

If you are using axios, put this when creating the instance:

const instance = axios.create({withCredentials: true, baseURL: API_URL});
Kifoxive
  • 1
  • 1