Search code examples
vue.jsexpresscookieslocalhostvite

Problem with setting cookies on localhost during development


I have nodejs express app as backend, and vue app as frontend on my server. I use session cookies for authentication and i just can't get them working on my local dev machine. Cookie is sent in the response but it is not saved by browser. I know that problem is that request origin must be same as host and i have read all the articles/questions related to the issue and i just can't solve it.

On the server, everything is proxied with nginx on https. Cookie is set there and everything works fine, because they are on same host.

My local vite dev server is running on https, on port 5173.

I use axios for api calls and i have set

axios.defaults.withCredentials = true;

Cookie properties are

sameSite:'none', 
secure:true,
httpOnly:true,

In express:

app.use(
  cors({
    origin: ['https://localhost:5173', 'https://my.site.com'],
    credentials: true,
  })
); 

Like i said before, cookie is sent in the response from the server and just not stored on browser because in request header:

host:my.site.com
origin:https://localhost:5173
referer:https://localhost:5173/

How can i solve this?


Solution

  • I followed solution in comments on https://stackoverflow.com/a/46412839/13781306. It partialy works when i set

    sameSite:'none', 
    secure:false,
    

    Cookie is passed in requests but it is not stored in the browser storage/cookies. Which is fine and solves my issue.