{"resource":{"author":{"id":"h2so9H8jPMmgUKGghoNl","name":"Yomesh Gupta","username":"yomeshgupta"},"content":{"link":null,"difficulty":1,"domain":2,"type":1,"isInternal":true,"body":"In this blog post, we are going to see how can we send multiple cookies to the browser in a Remix powered web app.\r\n\r\n## Problem Statement\r\n\r\nLet us consider the `login` functionality in a web app. Once the user is successfully authenticated, we want to --\r\n\r\n1. Redirect users to their respective dashboard\r\n2. Set the session cookie\r\n3. Set the tracking cookie\r\n\r\n## Setting up Session and Redirection\r\n\r\nWe can use Remix's inbuilt utilities to manage the session and redirect.\r\n\r\n```js\r\nimport { createCookieSessionStorage, redirect } from \"remix\";\r\n\r\nconst storage = createCookieSessionStorage({ ... });\r\n\r\nexport async function createUserSession(userId) {\r\n  const session = await storage.getSession();\r\n  session.set(\"userId\", userId);\r\n\r\n  return redirect('/dashboard');\r\n}\r\n```\r\n\r\nIn the function `createUserSession`, we get the session, set the `userId`, and redirect the user to the dashboard route.\r\n\r\n## Setting Cookies\r\n\r\n1. The HTTP helper `redirect` provided by Remix takes an object as the second parameter where we can set different options like `status`, `headers`, and so on. Let us use that to send the `Set-Cookie` header with the session cookie as the value.\r\n\r\n```js\r\n...\r\n\r\nreturn redirect('/dashboard', {\r\n  headers: {\r\n    'Set-Cookie': await storage.commitSession(session)\r\n  }\r\n});\r\n```\r\n\r\nWe are now redirecting the user to the dashboard and also setting the `session` cookie to keep them logged in.\r\n\r\n2. Now, we want to set the tracking cookie. We can try doing the same thing as the session cookie and set another `Set-Cookie` header.\r\n\r\n```js\r\n...\r\n\r\nreturn redirect('/dashboard', {\r\n  headers: {\r\n    'Set-Cookie': await storage.commitSession(session),\r\n    'Set-Cookie': 'user_tracking_id=e8bb43229de9; Max-Age=2592000; Path=/; Secure; SameSite=Lax'\r\n  }\r\n});\r\n```\r\n\r\nHowever, the above code snippet introduces a new problem. The most recent `Set-Cookie` key would overwrite the previous ones.\r\n\r\n## Resolving Same Header Key Issue\r\n\r\nWe can resolve the above-mentioned problem by using the [Headers()](https://developer.mozilla.org/en-US/docs/Web/API/Headers/Headers) constructor. We can create a new instance of the Headers object, append keys to it, and send it to the client.\r\n\r\n```js\r\n...\r\n\r\nconst headers = new Headers();\r\nheaders.append(\"Set-Cookie\", await storage.commitSession(session));\r\nheaders.append('Set-Cookie': 'user_tracking_id=e8bb43229de9; Max-Age=2592000; Path=/; Secure; SameSite=Lax');\r\n\r\nreturn redirect('/dashboard', {\r\n  headers,\r\n});\r\n```\r\n\r\nThis resolves our problem and both cookies would be stored in the user browser! 🙌\r\n\r\nI hope this blog post helped you in some way. Please do share it and show our content much-needed love! 😄\r\n\r\nIf you feel something is missing/wrong/improvement is possible then feel free to reach out -- [Devtools Tech](https://twitter.com/devtoolstech) and [Yomesh Gupta](https://twitter.com/yomeshgupta)\r\n","languages":[],"editorConfig":{}},"stats":{"views":17432,"used":0,"likes":0},"description":"Set more than one cookie in a single Remix response, such as a session cookie and a tracking cookie.","published":true,"isActive":true,"tags":["","javascript","cookies","remix run","js framework","web fundamentals","frontend","coding","headers","http","redirection","tracking","session"],"slug":"how-to-send-multiple-cookies-in-a-remix-powered-web-app---rid---ndORf220mRIUxvoJPvtb","isPremium":false,"categories":["backend","tooling"],"requires":[],"_id":"6206280b1641361c7c65a721","title":"How to Send Multiple Cookies in a Remix Powered Web App?","resourceId":"ndORf220mRIUxvoJPvtb","createdAt":1644570635295,"modifiedAt":1644571041887},"currentUser":null,"isOwner":false,"recommendations":{"questions":[{"_id":"636b87fa20622762d08cede0","content":{"languages":["javascript","typescript"],"difficulty":1},"tags":["","javascript","arrays","javascript polyfills","interview question","frontend","coding","devtools tech","programming","codedamn","frontend masters","egghead","razorpay","hotstar","tata1mg","swiggy","zomato","array","custom unshift","array unshift","array pop"],"slug":"how-to-implement-array-prototype-pop-javascript-interview-question-or-problem-solving-or-javascript-polyfills---qid---lujToJoIqX1KHvWPiqQf","title":"How to implement Array.prototype.pop? JavaScript Interview Question | Problem Solving | JavaScript Polyfills","questionId":"lujToJoIqX1KHvWPiqQf"},{"_id":"5f6c9dd26d3cda64e470c3f8","content":{"difficulty":1,"languages":"javascript"},"tags":["javascript","new operator","js fundamentals","nodejs"],"slug":"what-would-be-the-output-new-operator-or-explicit-primitive-return---qid---mGk0SsTTqHmfwxtNaIK1","title":"What would be the output? [New Operator | Explicit Primitive Return]","questionId":"mGk0SsTTqHmfwxtNaIK1"},{"_id":"64270fcb6577005718b7c555","content":{"languages":["react","html"],"difficulty":1},"tags":["javascript","frontend","ui","ux","figma","login page design","frontend projects","devkode","real world interview question","practice project","devtools tech"],"slug":"design-a-login-page-or-version-1-or-react-js-or-html-or-css-or-javascript-or-frontend-projects-for-beginners---qid---XbwRiIWYgOl8DpgCqwx6","title":"Design a Login Page | Version 1 | React.js | HTML | CSS | JavaScript | Frontend Projects for Beginners","questionId":"XbwRiIWYgOl8DpgCqwx6"},{"_id":"6304a59777f9961d5b7cb002","content":{"languages":["javascript","typescript"],"difficulty":2},"tags":["javascript","lodash","pick","frontend","problem solving","interview questions","egghead","frontend masters","advanced js","library","ui","underscore"],"slug":"implement-lodash-pick-from-scratch-or-problem-solving-or-frontend-interview-question---qid---5Q0SKd6PFVE7Kfkw0F1A","title":"Implement lodash.pick from scratch | Problem Solving | Frontend Interview Question","questionId":"5Q0SKd6PFVE7Kfkw0F1A"},{"_id":"6255355d3ecc8e41a79f0832","content":{"languages":["javascript","typescript"],"difficulty":1},"tags":["","javascript","format","currency","frontend","interview","problem solving","leetcode"],"slug":"implement-a-currency-formatting-utility-or-javascript-interview-question---qid---ZWwL12l2uqKZOQSAGvrl","title":"Implement a Currency Formatting Utility | JavaScript Interview Question","questionId":"ZWwL12l2uqKZOQSAGvrl"}],"resources":[{"_id":"6aeefb686f21da5c6ca90b5d","content":{"difficulty":2,"domain":1,"type":1,"isInternal":true,"languages":[]},"tags":[],"slug":"how-i-debugged-and-fixed-a-memory-leak-in-my-node-js-dev-server---rid---WHuxz0Uwbb3tuPoEL8lR","title":"How I debugged and fixed a memory leak in my Node.js dev server","resourceId":"WHuxz0Uwbb3tuPoEL8lR"},{"_id":"69a52acc92f33c7d8a9f6107","content":{"difficulty":4,"domain":1,"type":2,"isInternal":false,"languages":[]},"tags":["javascript","ui","ux","devtools tech","tutorial","rendering","coding"],"slug":"how-react-ssr-actually-works-without-frameworks---rid---8decG2azJUycVwuG2WQE","title":"How React SSR Actually Works (Without Frameworks)","resourceId":"8decG2azJUycVwuG2WQE"},{"_id":"6924ac08bf1a48f85e0ddb2d","content":{"difficulty":4,"domain":1,"type":2,"isInternal":false,"languages":[]},"tags":["javascript","promises","frontend","ui","ux","devtools tech","coding","async await","async programming","coding"],"slug":"javascript-promises-complete-course---basics-to-advanced---rid---2lf744w157vmmYfDP14Q","title":"JavaScript Promises Complete Course – Basics to Advanced","resourceId":"2lf744w157vmmYfDP14Q"},{"_id":"6339359342e7761bfac16e89","content":{"difficulty":1,"domain":2,"type":2,"isInternal":false},"tags":["javascript","requestly","frontend","devtools tech","coding","mock server","mock api","headers","redirect","api"],"slug":"how-to-supercharge-your-development-workflow-or-reviewing-requestlyio-or-improving-frontend-dx---rid---vgQNMcZkgncuDninBiYf","title":"How to Supercharge Your Development Workflow!? | Reviewing Requestly.io | Improving Frontend DX","resourceId":"vgQNMcZkgncuDninBiYf"},{"_id":"652d64e4d5ab2876a4ca213c","content":{"difficulty":4,"domain":2,"type":1,"isInternal":false,"languages":["undefined"]},"tags":["frontend","coding","interviews","experience","devtools tech","blog","medium"],"slug":"zeta-interview-experience-sde-1-frontend---rid---2RwNAPVBDsL25iOQe1l0","title":"Zeta Interview Experience SDE-1 FrontEnd","resourceId":"2RwNAPVBDsL25iOQe1l0"}]}}