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.

The cookie doesn't allow response on production environment

See original GitHub issue

The scenario is when the backend service based Koajs and Koa-session with the default configuration:

app.keys = [process.env.SESSION_SECRET];
const isProd = process.env.NODE_ENV === 'production' ? true : false;
const SESSION_CONFIG: any = {
  key: 'koa:sess',
  maxAge: 1000 * 60 * 60 * 24 * 7, // 7 days
  overwrite: true,
  httpOnly: isProd,
  signed: true,
  rolling: true,
  renew: false,
  secure: isProd,
  sameSite: 'None',
};

When the request calling to the endpoint and get an error code 500, it doesn’t respond as expected and set the cookie to the browser. But when removing secure and sameSite, it’s working but the cookie doesn’t set to the browser.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
phivhcommented, Dec 31, 2020

Thanks @dominicegginton , The solution above was helpful and we also can set domain: 'api.domain.com' in the configuration. Maybe this is also a session to learn. const SESSION_CONFIG = { key: 'koa:sess', maxAge: 1000 * 60 * 60 * 24 * 7, // 7 days overwrite: true, httpOnly: isProd, signed: true, rolling: true, renew: false, secure: isProd, sameSite: 'None', domain: 'api.anotherdomain.com', } Thanks for your help!

4reactions
hta218commented, Aug 24, 2021

Thanks all, I already fixed the issue.

I’ll re-format the answer of @phivh to make it more readable

const isProd = process.env.NODE_ENV === 'production' ? true : false

const CONFIG = {
  key: 'koa.sess',
  maxAge: 86400000,
  autoCommit: true, 
  overwrite: true, 
  httpOnly: isProd, 
  signed: true, 
  rolling: false, 
  renew: false, 
  secure: isProd,
  sameSite: 'None',
};

app.use(koaSession(CONFIG, app));

Read more comments on GitHub >

github_iconTop Results From Across the Web

The cookie doesn't allow response on production environment
The cookie doesn't allow response on production environment. This issue has been tracked since 2020-12-28. The scenario is when the backend service based ......
Read more >
Cookies are not being set in production - Stack Overflow
Most likely, The Right domain is not associated with the cookie being set so while setting the cookie, the browser silently rejects the...
Read more >
7 Keys to the Mystery of a Missing Cookie - Medium
Check out the OPTIONS response header ACCESS-CONTROL-ALLOW-CREDENTIAL whether it is set to true . If the server doesn't allow credentials being ...
Read more >
Response cookies not being set · Issue #386 · github/fetch
I'm trying to implement client login using fetch on react. I'm using passport for authentication. The reason I'm using fetch and not regular ......
Read more >
Everything You Need to Know About Cookies for Web ...
Cookies with HttpOnly can only be accessed by the server, and not by the browser's Document.cookie API. This is perfect for things like...
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