diff --git a/README.md b/README.md index 846a57ec..f181f7e9 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,12 @@ var orejimeConfig = { // defaults to "orejime". cookieName: "orejime", - // Optional. You can set a custom expiration time for the Orejime cookie, in days. + // Optional. You can set a custom expiration time for the Orejime cookie, in days, + // by using an integer or a function returning an integer. + // The function has the following signature: `function cookieExpiresAfterDays (consents, config): integer` + // - consents is an object of apps name defined in `config.apps[].name` with their consent value as a boolean (same as the cookie value). + // - config is your defined configuration (this object). + // You can find a snippet function here: https://github.com/empreinte-digitale/orejime/pull/79#issue-1100817288 // defaults to 365. cookieExpiresAfterDays: 365, diff --git a/src/consent-manager.js b/src/consent-manager.js index d0c40d92..df071c7f 100644 --- a/src/consent-manager.js +++ b/src/consent-manager.js @@ -127,11 +127,17 @@ export default class ConsentManager { saveConsents() { if (this.consents === null) deleteCookie(this.cookieName); const value = this.config.stringifyCookie(this.consents); + const configCookieExpiresAfterDays = this.config.cookieExpiresAfterDays; + + const cookieExpiresAfterDays = + typeof configCookieExpiresAfterDays === 'function' + ? configCookieExpiresAfterDays(this.consents, this.config) + : configCookieExpiresAfterDays || 120; setCookie( this.cookieName, value, - this.config.cookieExpiresAfterDays || 120, + cookieExpiresAfterDays, this.config.cookieDomain );