I can't set cookie on localhost

99封情书 提交于 2020-12-12 03:18:09

问题


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.

来源:https://stackoverflow.com/questions/59099773/i-cant-set-cookie-on-localhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!