Skip to content

Commit

Permalink
Merge pull request #837 from infinum/feature/domain-argument-in-setco…
Browse files Browse the repository at this point in the history
…okie

Support an optional `domain` argument in setCookie helper
  • Loading branch information
mbmjertan authored Jul 31, 2024
2 parents 298d666 + f878ed1 commit 253fa10
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 253fa10

Please sign in to comment.