From f878ed1c246b55a15502624e23888aaa79fdc0ab Mon Sep 17 00:00:00 2001 From: Mario Borna Mjertan Date: Tue, 30 Jul 2024 16:43:25 +0200 Subject: [PATCH] Support an optional `domain` argument in setCookie helper 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. --- scripts/helpers/cookies.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/scripts/helpers/cookies.js b/scripts/helpers/cookies.js index bf0987c77..d42d48e8f 100644 --- a/scripts/helpers/cookies.js +++ b/scripts/helpers/cookies.js @@ -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()}`; }, /**