Home > Mobile >  Understanding how cookie is set on the browser
Understanding how cookie is set on the browser

Time:12-05

I'm using express-session to initialize a session and save the cookie. But the process of how the cookie is saved browser side is abstracted away and something of a black box to me, it just happens automatically. Can anyone point to a resource that explains how the client takes the cookie from the response and saves it in local storage? My front facing stack is composed of react, nextjs and urql client.

CodePudding user response:

When you use express-session to initialize a session and save the cookie on the server, the client automatically receives the cookie in the response from the server and saves it in the local storage. This happens because the browser automatically includes the cookie in the request headers for any subsequent requests to the same domain, and the server uses the cookie to identify the user's session.

The process of how the cookie is saved in the local storage and included in the request headers is part of the underlying mechanics of the HTTP protocol and is handled automatically by the browser. It is not something that you need to worry about or configure when using express-session.

If you want to learn more about how cookies work in general, you can check out the following resources:

  1. The official documentation for cookies on the Mozilla Developer Network: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies
  2. A tutorial on cookies from the W3Schools website: https://www.w3schools.com/js/js_cookies.asp
  • Related