0

I have an express code that sends the cookie to get requests in some route:

res.cookie("userID", `${userSaved._id}`, { maxAge: 900000, httpOnly: true });

So when I make a get request in this route the response header has Set-Cookie : USERID : 234232,

I have another route where I want to use this cookie, but on the request header I don't see any cookie persisted. I'm using React as a front-end and node as a backend.

I have tried modifying my cors middleware, In react in fetch() I have put credentials : include,

1

1 Answer 1

0

There are 2 types of cookie, one for Session cookie, much like the SessionStorage, will remove while the client is closed. one for Permanent cookie, removed at a specific date (Expires) or after a specific length of time (Max-Age) and not when the client is closed.

I would suggest you to check the response header, whether returns like this:

Set-Cookie: userID=xxxx; maxAge=900000

The MDN describe like this:

  • Session cookie

Session cookies are removed when the client shuts down. Cookies are session cookies if they do not specify the Expires or Max-Age attribute.

Set-Cookie: sessionId=38afes7a8
  • Permanent cookie

Permanent cookies are removed at a specific date (Expires) or after a specific length of time (Max-Age) and not when the client is closed.

Set-Cookie: id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT
Set-Cookie: id=a3fWa; Max-Age=2592000
Sign up to request clarification or add additional context in comments.

5 Comments

It is being returned as a permanent cookie but it did not persist with the post requests that i make.
How did you send your post requests? please give more detail information.
Fetch (url,{mode:’cors’,method:’Post’, headers:{content-type : ‘json/application’)})
If a cors request is made, the credentials option should be setted. view the information here: developer.mozilla.org/en-US/docs/Web/API/…
Yes, I set the credentials and it worked thanks Jacy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.