Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

$cookieStore not working in IE or Safari #1191

Closed
patrickjamesbarry opened this issue Jul 20, 2012 · 17 comments
Closed

$cookieStore not working in IE or Safari #1191

patrickjamesbarry opened this issue Jul 20, 2012 · 17 comments

Comments

@patrickjamesbarry
Copy link

I just finished creating my first angular app, and submitting this bug out of memory. Using AngularJS 1.0.1. My app checks for presence of params stored in session cookies, via $cookieStore. If the params were not found, then user was redirected using $location service to the login page. This worked great in Chrome and firefox, but is broken in IE 8 and I believe Safari (latest). I had to abandon the $cookieStore and use jStorage instead.

@dansmithy
Copy link

This is also raised as issue 1090 (#1090). Can this get labelled as a bug?

@JensRantil
Copy link
Contributor

I am too experiencing this issue on Safari or Internet Explorer 9 with angularJS and angularjs-cookie 1.0.1. Setting and retrieving value works fully in Chromium 18.0.1025.151 and Firefox 14.0.1. Not sure whether it's setting or retrieving value that fails. Is it possible to list cookies set in Safari?

My specific usecase is:

  1. Load controller for a page that has a checkbox bound to an ng-model. In controller;
    1. If $cookies.mycookie is set to 'true' change ng-model value to true, else false.
    2. Watch the ng-model using a callback. Set a cookie using the $cookies factory on change. If checkbox is checked, store 'true', else 'false'.
  2. Check the checkbox (that default to unchecked).
  3. Leave the page and go back to it. Expected result is checkbox to be checked. It is not in Safari and IE 9.

@JensRantil
Copy link
Contributor

FYI; I am currently using jStorage (as mentioned in #1090) until a workaround for this has been established.

@levyuk
Copy link

levyuk commented Sep 20, 2012

Any further information on this? I think the issue is probably with the cookieStore.put method.

@pchronz
Copy link

pchronz commented Oct 14, 2012

+1 major issue if you rely on angular from my point of view.

@SET001
Copy link

SET001 commented Oct 30, 2012

+1, still an issue!

@vasa-chi
Copy link

+1 major issue!

@aziz
Copy link

aziz commented Oct 30, 2012

I also couldn't get the $cookieStore to work for me. went back to jQuery plugins for now! :(

@nilnullzip
Copy link

Found it. The problem occurs in this line in browser.cookie (in angular.js) because cookiePath is undefined:

    cookieLength = (rawDocument.cookie = escape(name) + '=' + escape(value) + ';path=' + cookiePath).length + 1;

The resulting string ends in "path=undefined". This string is passed to the native document.cookie setter function. It seems that Safari and Chrome have different behavior here and Chrome is more forgiving. Chrome treats the string as if it ended as "path=" and assigns the cookie accordingly, while Safari silently ignores the cookie assignment and leaves the cookie unchanged.

The fix/workaround can be applied a few lines earlier to where cookiePath is assigned:

var cookiePath = self.baseHref();

Add the line below so that cookiePath will become "" instead of "undefined"

var cookiePath = self.baseHref();
if (!cookiePath) {cookiePath = ""}

NOTE: the reason cookiePath can be undefined is because the code derives it from the tag in the document. The undefined result occurs when no tag is specified. The thing I don't understand is whether it is desirable to always set path from base. It seems perhaps a non-obvious consequence of setting .

@frapontillo
Copy link
Contributor

The workaround works and is needed, why don't you make a pull request?

@jkeys089
Copy link

+1

2 similar comments
@jfleong
Copy link

jfleong commented Jan 15, 2013

+1

@bwu
Copy link

bwu commented Jan 22, 2013

+1

@lucsky
Copy link

lucsky commented Jan 23, 2013

+1, currently having to use the jquery.cookie plugin because of this.

@DavidFrahm
Copy link

+1, and fyi this is broken in Safari in iOS also

@gflarity
Copy link

gflarity commented Feb 6, 2013

+1

Thanks Fredrik, I just verified this fix worked for me. Please merge this, it's a pretty severe bug!

IgorMinar pushed a commit to IgorMinar/angular.js that referenced this issue Feb 7, 2013
Safari and IE don't like being told to store cookies with path set to
undefined. This change ensures that if base[href] (from which cookie path
is derived) is undefined then the cookie path defaults to ''.

The test verifies that the cookie is set instead of checking that cookie has correct path,
this is due to that cookie meta information is not avabile once the cookie is set.

Closes angular#1190, angular#1191
IgorMinar pushed a commit that referenced this issue Feb 7, 2013
Safari and IE don't like being told to store cookies with path set to
undefined. This change ensures that if base[href] (from which cookie path
is derived) is undefined then the cookie path defaults to ''.

The test verifies that the cookie is set instead of checking that cookie has correct path,
this is due to that cookie meta information is not avabile once the cookie is set.

Closes #1190, #1191
IgorMinar pushed a commit that referenced this issue Feb 7, 2013
Safari and IE don't like being told to store cookies with path set to
undefined. This change ensures that if base[href] (from which cookie path
is derived) is undefined then the cookie path defaults to ''.

The test verifies that the cookie is set instead of checking that cookie has correct path,
this is due to that cookie meta information is not avabile once the cookie is set.

Closes #1190, #1191
@IgorMinar
Copy link
Contributor

fixed by 7cb8f8f

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests