I have a cookie save a token when a user logs into www.example.com and then it redirects them to example.com/desktop or example.com/mobile depending on what device they\'re usin
Because the cookie was set at another path, you have to use Cookies.removeCookie("cookieName", "/")
(/
being the path used in your example) and not Cookies.removeCookie("cookieName")
.
This is because without a specified path, the path defaults to the one of the current page (see document.cookie).
So, you're trying to remove the cookie at path=/desktop, whereas it's actually at path=/, so the removal fails.
Remember that you could have two cookies with the same name but different paths; so you could have a cookieName at path=/
and a _cookieName at path=/desktop
. Removing the cookie at path=/
won't remove the one at path=/desktop
, and conversely, removing the one at path=/desktop
won't remove the one at path=/
.
As a side note: when accessing /desktop
, the browser would send both cookies, which could have different values.
In brief, because you set your cookie at /
, remember to always pass /
as the path, everywhere, or you could create a new cookie rather than modifying the existing one, or fail to remove it (which you're experiencing right now).
See also Cookies.removeCookie(String,String)
In case you also need a non-default domain use
public static native void removeCookie(String name, String path, String domain) /*-{
$doc.cookie = name + "=" + ((path) ? ";path=" + path : "")
+ ((domain) ? ";domain=" + domain : "")
+ ";expires=Thu, 01 Jan 1970 00:00:01 GMT";
}-*/;