orestbida / cookieconsent

:cookie: Simple cross-browser cookie-consent plugin written in vanilla js
https://playground.cookieconsent.orestbida.com/
MIT License
3.85k stars 405 forks source link

Cookies are not deleted when deactivating a group of cookies #35

Closed tur0o closed 3 years ago

tur0o commented 3 years ago

Hello, While wanting to try this plugin, I encountered a problem.

When i put in the options "auto clear cookies: true", the cookies are not deleted when you deactivate a group of cookies from the panel. And I understood why.

In my case, when cookies are first created, the domain stored in the browser's cookies section is the primary domain ".mydomain.com".

On the other hand, when I want to disabled them, the code tries to identify them by a different domain values, sometimes including the subdomain (ex: .www.mydomain.com).

And since it cannot find them in existing cookies, it does not delete them.

I'm pretty bad at JS, but I fixed my problem temporarily by adding the 4 lines below while waiting for a possible fix from you.

var _eraseCookies = function(cookies, custom_path) {
            var path = custom_path ? custom_path : '/';
            var expires = 'Expires=Thu, 01 Jan 1970 00:00:01 GMT;';

            for(var i=0; i<cookies.length; i++){
                document.cookie = cookies[i] +'=; Path='+ path +'; Domain=' + _config.cookie_domain + '; ' + expires;
                document.cookie = cookies[i] +'=; Path='+ path +'; Domain=.' + _config.cookie_domain + '; ' + expires;

                // START - PLUGIN OVERWRITE : add the main domain too (without subdomain)
                var domainSplit = _config.cookie_domain.split('.');
                var mainDomain = domainSplit[domainSplit.length - 2] + '.' + domainSplit[domainSplit.length - 1]
                document.cookie = cookies[i] +'=; Path='+ path +'; Domain=' + mainDomain + '; ' + expires;
                document.cookie = cookies[i] +'=; Path='+ path +'; Domain=.' + mainDomain + '; ' + expires;
                // END - PLUGIN OVERWRITE

                _log("CookieConsent [AUTOCLEAR]: deleting cookie: '" + cookies[i] + "' path: '" + path + "'");
            }
        }

Thank you for your help :)

orestbida commented 3 years ago

Hi @3615arturo,

sure, looks like a safe change to me. Gonna include this in the upcoming revision (2.4.6)!