Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clear method doesnt work #152

Open
cahyowhy opened this issue Jan 18, 2018 · 7 comments
Open

clear method doesnt work #152

cahyowhy opened this issue Jan 18, 2018 · 7 comments
Labels

Comments

@cahyowhy
Copy link

cahyowhy commented Jan 18, 2018

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..

@marcoow
Copy link
Member

marcoow commented Jan 18, 2018

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

@marcoow marcoow added the triage label Jan 18, 2018
@eighthjouster
Copy link

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>

@eighthjouster
Copy link

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.

@marcoow marcoow added bug and removed triage labels Apr 4, 2018
@marcoow
Copy link
Member

marcoow commented Apr 4, 2018

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

@trdp30
Copy link

trdp30 commented Jul 18, 2019

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()});
}

@marcoow
Copy link
Member

marcoow commented Aug 2, 2019

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

@wagenet
Copy link

wagenet commented Feb 20, 2020

I'm seeing issue with this in Chrome now.

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

No branches or pull requests

5 participants