From f1c17be4a98832f52967fa0d433c64913fc8c2e4 Mon Sep 17 00:00:00 2001 From: Amit Verma Date: Sat, 11 Oct 2025 14:13:34 +0530 Subject: [PATCH] refactor: improve Cookie Handling (#7129) * Improve Cookie Handling * Update lib/helpers/cookies.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update lib/helpers/cookies.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Jay Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- lib/helpers/cookies.js | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/lib/helpers/cookies.js b/lib/helpers/cookies.js index d039ac4..266f09d 100644 --- a/lib/helpers/cookies.js +++ b/lib/helpers/cookies.js @@ -5,27 +5,38 @@ export default platform.hasStandardBrowserEnv ? // Standard browser envs support document.cookie { - write(name, value, expires, path, domain, secure) { - const cookie = [name + '=' + encodeURIComponent(value)]; + write(name, value, expires, path, domain, secure, sameSite) { + if (typeof document === 'undefined') return; - utils.isNumber(expires) && cookie.push('expires=' + new Date(expires).toGMTString()); + const cookie = [`${name}=${encodeURIComponent(value)}`]; - utils.isString(path) && cookie.push('path=' + path); - - utils.isString(domain) && cookie.push('domain=' + domain); - - secure === true && cookie.push('secure'); + if (utils.isNumber(expires)) { + cookie.push(`expires=${new Date(expires).toUTCString()}`); + } + if (utils.isString(path)) { + cookie.push(`path=${path}`); + } + if (utils.isString(domain)) { + cookie.push(`domain=${domain}`); + } + if (secure === true) { + cookie.push('secure'); + } + if (utils.isString(sameSite)) { + cookie.push(`SameSite=${sameSite}`); + } document.cookie = cookie.join('; '); }, read(name) { - const match = document.cookie.match(new RegExp('(^|;\\s*)(' + name + ')=([^;]*)')); - return (match ? decodeURIComponent(match[3]) : null); + if (typeof document === 'undefined') return null; + const match = document.cookie.match(new RegExp('(?:^|; )' + name + '=([^;]*)')); + return match ? decodeURIComponent(match[1]) : null; }, remove(name) { - this.write(name, '', Date.now() - 86400000); + this.write(name, '', Date.now() - 86400000, '/'); } }