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

feat(#78): allow cookieExpiresAfterDays as a function #79

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
8 changes: 7 additions & 1 deletion src/consent-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
felixgirault marked this conversation as resolved.
Show resolved Hide resolved

Expand Down