question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there any way to set cookie domain per request rather than per server launch?

See original GitHub issue

Right now I have the following code in my index.js:

const Koa = require('koa');
const session = require('koa-session');

const app = new Koa();

app.use(
  session(
    {
      key: SSID,
      maxAge: SESSION_TTL,
      rolling: true,
      httpOnly: true,
      store: SessionStore,
      domain: DOMAIN,
    },
    app
  )
);

The problem is, it sets domain once and for all and then passes that value to ctx.cookie.set() every time. What I need is customizable cookie domain based on request values like hostname or query etc. I can elaborate on the use case I need that for, if that’s of any relevance. I don’t see how can I do that currently, but I found a solution which basically creates new koa-session middleware on every request:

app.use(async (ctx, next) => {
  const { cookieDomain } = ctx.query;

  await session(
    {
      key: SSID,
      maxAge: SESSION_TTL,
      rolling: true,
      httpOnly: true,
      store: SessionStore,
      domain: cookieDomain || DOMAIN,
    },
    app
  )(ctx, next);
});

This would work, but unfortunately 2 out of 3 properties defined here have configurable set to false implicitly, so on the second request I get “TypeError: Cannot redefine property: sessionOptions” error.

So, I have 3 questions:

  1. Is there any way to achieve what I need with koa-session right now that I’ve missed?

  2. If previous answer is “no”, is there any real reason sessionOptions and [CONTEXT_SESSION] are non-configurable?

  3. If previous answer is “no”, can it be changed to configurable? What needs to be done for that (do I need to submit PR or something)?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:4
  • Comments:5

github_iconTop GitHub Comments

2reactions
olsocommented, Dec 29, 2019

What prevents you from wrapping it and forwarding ctx,next? This is per request @Kumagor0

Screenshot 2019-12-27 at 18 50 49

My issue with koa-session is that you have to pass the Koa instance into it.

0reactions
Kumagor0commented, Dec 27, 2019

@olso Does it? Because looking at the docs,

app.use(session({
    key: "SESSIONID",   //default "koa:sess"
}));

looks like setting all options once and for all.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set-Cookie - HTTP - MDN Web Docs
Indicates that the cookie is sent to the server only when a request is made with the https: scheme (except on localhost), and...
Read more >
Does every web request send the browser cookies?
No. Not every request sends the cookies. It depends on the cookie configuration and client-server connection.
Read more >
Cookies, document.cookie - The Modern JavaScript Tutorial
A domain defines where the cookie is accessible. In practice though, there are limitations. We can't set any domain. There's no way to...
Read more >
Ultimate Guide to HTTP Cookies - webf
Cookie is not shared among different browsers. Means, one browser cannot read the cookie stored by another browser even if it is same...
Read more >
A practical, Complete Tutorial on HTTP cookies
To properly identify you on each subsequent request, the backend checks the cookie coming from the browser in the request. To send the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found