mainmatter/ember-cookies

clear method doesnt work

cahyowhy opened this issue · 7 comments

i try to clear user cookie in my ember app. and the cookie still there...

export default Service.extend({
  cookies: Ember.inject.service(),
  removeCookies() {
    let cookieService = this.get('cookies');
    cookieService.clear('user');
  }
});

its work in chrome. but not in firefox..
whats wrong..

Any Error? Is the cookie just not cleared? Does it have a path, domain, expiration etc.?

I can confirm that this is a bug, although it's not an ember-cookie bug. It's a Firefox bug (see: https://bugzilla.mozilla.org/show_bug.cgi?id=691973 ), and it's only related to cookies that are already expired.

Chrome isn't affected, so ember-cookies works flawlessly there.

Steps to reproduce:

  1. Create a cookie with an expiration timestamp 10 seconds from now.
  2. Wait until it expires.
  3. Check Firefox's cookies. The cookie will still be there.
  4. Attempt to delete cookie via Javascript. Nope. Not happening.

Here's a simple HTML (must be accessed through a local web server, or enable cookies with local files) that can help reproduce the problem (keep the dev console open in the Local Storage/Cookies section):

<html>
<body>

<input type="button" id="cookiebutton" onclick="cookieButton()" value="Create cookie"></button>
<input type="button" id="cookiedeletebutton" onclick="hardDeleteCookie()" value="HARD delete cookie"></button>
<div id="cookiemessage">No cookie (supposedly)</div>
<script>
  function cookieButton() {
    if (cookiebutton.value === 'Create cookie') {
      cookiebutton.value = 'Delete cookie';
      createCookie();
      setTimeout(announceExpiredCookie, 5000);
    }
    else if (cookiebutton.value === 'Delete cookie') {
      deleteCookie();
    }
  }
  
  function createCookie() {
    document.cookie = 'oneCookie=1;max-age=5;';
    cookiemessage.innerHTML = 'Cookie created.';
  }
  
  function deleteCookie() {
    document.cookie = 'oneCookie=1;expires=Thu, 01 Jan 1970 00:00:01 GMT';
    cookiemessage.innerHTML = 'Cookie deleted (supposedly)';
  }
  
  function hardDeleteCookie() {
    document.cookie = 'oneCookie=1;max-age=5;';
    document.cookie = 'oneCookie=1;expires=Thu, 01 Jan 1970 00:00:01 GMT';
    cookiemessage.innerHTML = 'Cookie hard deleted!';
  }
  
  function announceExpiredCookie() {
    cookiemessage.innerHTML = 'Cookie deleted (supposedly) or expired.';
  }

</script>

</body>
</html>

There is an easy workaround: Just before deleting the cookie, set it again to any value (an empty string should be fine). Firefox will of course "revive" the cookie, and clearing it will work at that point. For example:

// Let's delete the 'arepa' cookie:

    cookieService.write('arepa', '');
    cookieService.clear('arepa');

// The 'arepa' cookie is now gone.

Thanks for the details @eighthjouster! I just confirmed the respective test breaks in Firefox.

This issue is happening for expiry time, which is not getting set properly. Facing this issue in chrome also.
I'm doing this for clearing, which cleared on expired.

if(cookies.read([key])) {
  cookies.write([key], '', { path: '/', expires: moment().toDate()});
}

@trdp30 how is that cookie written in the first place?

I'm seeing issue with this in Chrome now.