Skip to content

Commit

Permalink
Support an optional domain argument in setCookie helper
Browse files Browse the repository at this point in the history
Adds an optional `domain` argument to the setCookie helper so the cookie domain can be set.

Additionally, does a minor restructuring of how the `document.cookie` value is generated.
  • Loading branch information
mbmjertan authored Jul 30, 2024
1 parent 298d666 commit f878ed1
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions scripts/helpers/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,22 @@ export const cookies = {
* cookies.setCookie('gdpr', '2', cookies.setOneDay(), '/');
* ```
*/
setCookie(key, value, time, path) {
setCookie(key, value, time, path, domain) {
const expires = new Date();
expires.setTime(expires.getTime() + (time));

let pathValue = '';
let domainValue = '';

if (typeof path !== 'undefined') {
pathValue = `path=${path};`;
pathValue = `;path=${path}`;
}

document.cookie = `${key}=${value};${pathValue}expires=${expires.toUTCString()}`;
if (typeof domain !== 'undefined') {
domainValue = `;domain=${domain}`;
}

document.cookie = `${key}=${value}${pathValue}${domainValue};expires=${expires.toUTCString()}`;
},

/**
Expand Down

0 comments on commit f878ed1

Please sign in to comment.