dobarkod / cookie-banner

JavaScript based cookie-info banner for complying with EU cookie law
MIT License
425 stars 85 forks source link

Cookie Expire Date #66

Closed 3L1AS closed 6 years ago

3L1AS commented 6 years ago

Hi. I'm trying to customize the expire setting and set an expiration date on the cookie. How do I do this using the data-expires attribute? What value should it be if I for example would like the cookie to expire after 3 months. Should it be the time in milliseconds from now, or something like that?

webaddicto commented 6 years ago

According to the documentation:

expires - Cookie expiry date/time (defaults to Infinity aka Fri, 31 Dec 9999 23:59:59 GMT). There's basic support for specifying a callback function (more flexibility, must return one of Number, String, or Date -- see Cookies.set()). You can also just specify a valid UTC string.

It is managed in this code inside cookiebanner.js file:

        set: function (key, val, end, path, domain, secure) {
            if (!key || /^(?:expires|max-age|path|domain|secure)$/i.test(key)) {
                return false;
            }
            var expires = '';
            if (end) {
                switch (end.constructor) {
                    case Number:
                        expires = end === Infinity ? '; expires=Fri, 31 Dec 9999 23:59:59 GMT' : '; max-age=' + end;
                        break;
                    case String:
                        expires = '; expires=' + end;
                    break;
                    case Date:
                        expires = '; expires=' + end.toUTCString();
                    break;
                }
            }
            doc.cookie = encodeURIComponent(key) + '=' + encodeURIComponent(val) + expires + (domain ? '; domain=' + domain : '') + (path ? '; path=' + path : '') + (secure ? '; secure' : '');
            return true;
        },

The function Cookies.set() is used within agree option:

        agree: function() {
            this.cookiejar.set(this.options.cookie, 1, this.options.expires, this.options.cookiePath, (this.options.cookieDomain !== '' ? this.options.cookieDomain : ''), (this.options.cookieSecure ? true : false));
            return true;
        },

So expires can be a number, a string or a date.

An example (untested) may be to set it to 90 (number of days, equal to 3 months):

<script type="text/javascript" id="cookiebanner"
    src="https://cdn.jsdelivr.net/gh/dobarkod/cookie-banner@1.2.2/dist/cookiebanner.min.js"
    data-height="20px" data-position="top"
    data-expires="90"
    data-message="We use cookies to improve your browsing experience.">
</script>
AuscanAlliance commented 6 years ago

data-expires="90" doesn't work for me.

zytzagoo commented 6 years ago

When specified as a string (which when data- attributes are in place is almost the only option pretty much, at least currently), it should be the end date in GMT format. (see https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toUTCString)

Or some horrible ugly hack that might work due to the way the expires string is built currently:

<script... data-expires=";max-age=7890000">

Above is based on 7890000 seconds being "3 months". Whether that will work out-of-the-box is hard to say...

But, if we consider going the route of transforming data-expires into numbers (when it looks like a numeric thingy), we should probably use/assume seconds as unit, not days. Or?

webaddicto commented 6 years ago

I added this PR that fixes this behavior:

https://github.com/dobarkod/cookie-banner/pull/72

Tested and working fine with:

data-expires="Thursday, August 28, 2018, 1:52:31 AM" or data-expires="90"