ScottHamper / Cookies

JavaScript Client-Side Cookie Manipulation Library
The Unlicense
1.77k stars 169 forks source link

Cookies not setting on client when explicitly setting "domain" and "expires" options #50

Closed iisa closed 9 years ago

iisa commented 9 years ago

Hi there,

Was working with this library on Safari and Chrome (local environment) and was unsuccessful in setting cookies when I explicitly defined "domain" and "expires" options.

It is successful in setting cookie without options. Is this a known behavior or perhaps user error?

Would like to talk more, thanks and looking forward to your reply. Best, Isa

ScottHamper commented 9 years ago

Hey Isa,

I suspect this is user error, but could you provide some more information just to be sure?

If it's an issue with the library, then I need a reproducible test case that exhibits the issue. Otherwise, there is nothing I can do to help you.

Possible reasons for failure:

iisa commented 9 years ago

Hi Scott,

Thanks very much for checking in with me so quick. I am hoping it is just me being clumsy. Here is my scenario:

I have multiple containers under one IP, all of them should be able to parse the cookie via the browser. I am developing on Safari. The domain I am using is my local server.

So i am calling the function like this:

Cookies.set('ThisCookie', 'hello', {domain: 'local.dev.ip'})

ScottHamper commented 9 years ago

Hey Isa,

Is the page that is calling Cookies.set running under the "local.dev.ip" domain, or a sub-domain of it? If not, the set will not occur.

Consider the following domains: localhost.com www.localhost.com app.localhost.com first.app.localhost.com

From a page running under any of these domains, they can all call: Cookies.set('key', 'value', { domain: 'localhost.com' });

And the cookie will be visible to all four domains listed above.

However, from a page running under localhost.com, if the following is attempted: Cookies.set('key', 'value', { domain: 'www.localhost.com' })

No cookie will be set (the cookie won't exist for pages running under www).

For one final example, if a page running under first.app.localhost.com attempts: Cookies.set('key', 'value', { domain: 'app.localhost.com' })

Then the cookie will be visible to pages running under both app.localhost.com as well as first.app.localhost.com, but not to www.localhost.com or localhost.com.

So the rule of thumb seems to be: cookies can only be set for the current domain, or a super-domain of the current domain.

I'm going to close this issue, as there does not appear to be a problem with Cookies.js regarding explicit domains. However, this issue seems to demonstrate that implementing changes as proposed in Issue #49 could be a worthwhile addition.

iisa commented 9 years ago

Hi Scott, thanks! Definitely came to that conclusion, too. Thanks and stay rad. -Isa

kunokdev commented 7 years ago

Hello, I had similar issue (adding www. on start of the string caused error) which was failing silently. Is there any way to catch this error and log it?