0

I'm attempting to write a simple cookie using the AngularJS v1.5.x $cookies service. The dependencies are all correct, as I can read a cookie. However, neither write nor edit are working.

For example:

var blah = $cookies.get('mysite_auth');
console.log('old: ' + blah);

$cookies.put('mysite_auth', 'blech');

blah = $cookies.get('mysite_auth');
console.log('new: ' + blah);

... results in the following output:

old: ZmVhZDE ... =
new: ZmVhZDE ... =

No errors are thrown. The same occurs when attempting to write a new cookie:

$cookies.put('someCookie', 'yup');

I can write a cookie in my controller like so:

document.cookie = 'TEST=TESTVALUE';

What might be the cause of this?

Rich
  • 970
  • 2
  • 16
  • 42
isherwood
  • 58,414
  • 16
  • 114
  • 157
  • It might fail on older versions of IE. What browsers are you trying this on? – austin Jun 07 '16 at 16:53
  • Chrome latest stable. – isherwood Jun 07 '16 at 16:53
  • have you tried $cookieStore.put to put values ? – Don Jun 07 '16 at 16:53
  • No. My understanding is that `$cookieStore` is deprecated. – isherwood Jun 07 '16 at 16:54
  • I found this issue on GitHub that mentions `$cookie.put` works asynchronously. You might be reading the cookie before it's actually written. You can try setting a timeout to see if that's the case. https://github.com/angular/angular.js/issues/6411 – austin Jun 07 '16 at 16:55
  • Cookies don't appear in dev tools at all. I've used jQuery to write cookies before and am familiar with typical behavior. – isherwood Jun 07 '16 at 16:56
  • are you finding any difference between localhost and other dev environments as that's come up before? – Rich Jun 07 '16 at 17:00
  • Here's something odd: This doesn't work in a local HTML file, but it does on JSFiddle. https://jsfiddle.net/9nnypax5 – isherwood Jun 07 '16 at 17:51
  • If it's a local file, then you probably need to start Chrome with an extra flag if you want cookies to work. See http://stackoverflow.com/a/347997/1789724. – austin Jun 07 '16 at 18:17

2 Answers2

1

It turns out that Angular $cookies was writing the cookie, but we had a domain/path problem. Because our Angular apps live outside our back end app directory, the page URL doesn't match the server path. If I visited the actual path, while the page didn't properly load, the cookie was present.

We resolved the issue with this domain/path configuration:

$cookies.put('myCookie', true, {
    domain: '.mydomain.com',
    path: currentUrl,
    expires: exp
});
isherwood
  • 58,414
  • 16
  • 114
  • 157
-1

have you tried setting the expiration for your cookie?

e.g.

var dDate = new $window.Date(),
var dExp = new $window.Date(dDate.getFullYear(), dDate.getMonth()+1, dDate.getDate());

$cookies.put('someCookie','yup', {expires: dExp});

var cCookie = $cookies.get('someCookie');
console.log(cCookie);

just found a cookie thread actually which might help: How to set expiration date for cookie in AngularJS

Community
  • 1
  • 1
Rich
  • 970
  • 2
  • 16
  • 42