Js-cookie: Unable to persist cookie across subdomains

Created on 18 Sep 2015  Â·  3Comments  Â·  Source: js-cookie/js-cookie

Domain the cookie is set: http://login.localhost.org
Script used to set the cookie: Cookies.set('user', user, {expires: 365}, {domain: '.localhost.org'});
Note: User is JSON object

I then navigate to: http://test.localhost.org
Script used to produce cookie: console.log(Cookies.get());
Script produces a blank object: {}

I then navigate back to: http://login.localhost.org
Script used to echo cookie: console.log(Cookies.get());
Script produces the cookie: {user: "{"key":"value"}"}

What am I doing wrong?

Most helpful comment

It's no browser issue, there is a problem with the snippet you provided (I overlooked the first time):

Cookies.set('user', user, { expires: 365 }, { domain: '.localhost.org' });

As you can see above it is declaring 4 arguments, but js-cookie accepts only 3 (name, value and attributes). The domain attribute should be inside the same Object as expires:

Cookies.set('user', user, { expires: 365, domain: '.localhost.org' });

Then it is supposed to work.

All 3 comments

It seems that the browser is failing to handle the "dot" properly. Which browser are you using?

IE11 on Windows 10.

It's no browser issue, there is a problem with the snippet you provided (I overlooked the first time):

Cookies.set('user', user, { expires: 365 }, { domain: '.localhost.org' });

As you can see above it is declaring 4 arguments, but js-cookie accepts only 3 (name, value and attributes). The domain attribute should be inside the same Object as expires:

Cookies.set('user', user, { expires: 365, domain: '.localhost.org' });

Then it is supposed to work.

Was this page helpful?
0 / 5 - 0 ratings