From b526fc059fe2e3de4547d375fa37d5901a69f2e6 Mon Sep 17 00:00:00 2001 From: Katie Date: Wed, 17 Aug 2022 09:25:51 -0400 Subject: [PATCH 1/4] CSR-759 Update how session ID cookies are saved and updated --- .../heritage-integration/cookie-helper.js | 39 ++++++++++++++++--- src/mixins/analytics-mixin.js | 8 +++- src/router/index.js | 5 ++- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/helpers/heritage-integration/cookie-helper.js b/src/helpers/heritage-integration/cookie-helper.js index 775a64fec..69d4ee4b5 100644 --- a/src/helpers/heritage-integration/cookie-helper.js +++ b/src/helpers/heritage-integration/cookie-helper.js @@ -10,7 +10,7 @@ export function updateOrCreateFunnelCookie() { const shouldSuppressConceptFunnel = getFunnelCookie()?.SuppressConceptFunnel; // Create the cookie - document.cookie = `${cookieNames.FUNNEL_SESSION_INFO}={}; path=${applicationConfig.COOKIE_PATH}; ${getCookieDomainValue()};`; + document.cookie = `${cookieNames.FUNNEL_SESSION_INFO}={}; path=${applicationConfig.COOKIE_PATH}; ${getCookieDomainValue()}`; // Set up cookie with all the props. setFunnelCookieProperties({ @@ -106,10 +106,17 @@ export function getSessionIdValue(){ return '00000000-0000-0000-0000-000000000000'; } -export function setCookieProperties(properties) { +/* + Updates session ID cookie with new expiration date +*/ +export function updateSessionIdCookie() { + createCookie(cookieNames.SESSION_ID, getSessionIdValue(), { maxAge: 60 * 30 }); +} + +export function setCookieProperties(properties, { useDefaultFunnelCookieAttributes = true, maxAge }) { if (typeof properties == "object") { Object.keys(properties).forEach(key => { - document.cookie = `${key}=${properties[key]}`; + createCookie(key, properties[key], { useDefaultFunnelCookieAttributes, maxAge }); }); } } @@ -135,12 +142,34 @@ function setFunnelCookieProperties(properties) { }); const cookieValueJson = JSON.stringify(cookie); - - document.cookie = `${cookieNames.FUNNEL_SESSION_INFO}=${cookieValueJson}; path=${applicationConfig.COOKIE_PATH}; ${getCookieDomainValue()}`; + + createCookie(cookieNames.FUNNEL_SESSION_INFO, cookieValueJson, {}); } } } +/* + Used to create a cookie. + `useDefaultFunnelCookieAttributes` will set the path and domain to our defaults +*/ +function createCookie(key, value, { useDefaultFunnelCookieAttributes = true, maxAge, isSecure = true }) { + let cookieToAdd = `${key}=${value}; `; + console.log(isSecure) + console.log(useDefaultFunnelCookieAttributes) + + if (useDefaultFunnelCookieAttributes) { + cookieToAdd += `path=${applicationConfig.COOKIE_PATH}; ${getCookieDomainValue()} `; + } + if (isSecure) { + cookieToAdd += `secure; `; + } + if (maxAge) { + cookieToAdd += `max-age=${maxAge};`; + } + + document.cookie = cookieToAdd; +} + /* Gets current domain without the subdomain for cookie. */ diff --git a/src/mixins/analytics-mixin.js b/src/mixins/analytics-mixin.js index e24a55e36..f5535188b 100644 --- a/src/mixins/analytics-mixin.js +++ b/src/mixins/analytics-mixin.js @@ -122,10 +122,14 @@ export default { if (response.data) { if (response.data.sessionKey && skey === 0) { - setCookieProperties({ [cookieNames.SESSION_KEY]: response.data.sessionKey}); + setCookieProperties({ [cookieNames.SESSION_KEY]: response.data.sessionKey}, { + useDefaultFunnelCookieAttributes: false + }); } if (response.data.sessionId && sid === '00000000-0000-0000-0000-000000000000') { - setCookieProperties({ [cookieNames.SESSION_ID]: response.data.sessionId}); + setCookieProperties({ [cookieNames.SESSION_ID]: response.data.sessionId}, { + maxAge: 60 * 30 // 30 minutes + }); } } }, diff --git a/src/router/index.js b/src/router/index.js index 6fcd48c28..e2c1fe4d1 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -10,7 +10,7 @@ import { getDeviceIdValue } from "@/helpers/heritage-integration/cookie-helper"; // Heritage integration import { isSavedSessionStillActive } from "@/helpers/heritage-integration/session-helper"; -import { updateOrCreateFunnelCookie, getFunnelCookie } from "@/helpers/heritage-integration/cookie-helper"; +import { updateOrCreateFunnelCookie, getFunnelCookie, updateSessionIdCookie } from "@/helpers/heritage-integration/cookie-helper"; import { loadOrderIfPresent, saveOrder } from "@/helpers/heritage-integration/order-helper"; import { getPageToRouteExistingOrderTo, navigateToHeritageFunnel } from "@/helpers/heritage-integration/navigation-helper"; @@ -31,6 +31,9 @@ const routes = [ if (analyticsMixin.methods.noSession()) { await analyticsMixin.methods.initSession(); } + else { + updateSessionIdCookie(); + } if (getFunnelCookie()?.SuppressConceptFunnel) { await navigateToHeritageFunnel(false); From 61ccfe268b522b395106cd19203b3c79fc421552 Mon Sep 17 00:00:00 2001 From: Katie Date: Wed, 17 Aug 2022 11:32:54 -0400 Subject: [PATCH 2/4] CSR-111 Fix tests and cleanup --- .../heritage-integration/cookie-helper.js | 33 ++++++++++--------- src/helpers/unit-test-helper.js | 5 +-- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/helpers/heritage-integration/cookie-helper.js b/src/helpers/heritage-integration/cookie-helper.js index 69d4ee4b5..f6f44e3e4 100644 --- a/src/helpers/heritage-integration/cookie-helper.js +++ b/src/helpers/heritage-integration/cookie-helper.js @@ -10,7 +10,8 @@ export function updateOrCreateFunnelCookie() { const shouldSuppressConceptFunnel = getFunnelCookie()?.SuppressConceptFunnel; // Create the cookie - document.cookie = `${cookieNames.FUNNEL_SESSION_INFO}={}; path=${applicationConfig.COOKIE_PATH}; ${getCookieDomainValue()}`; + // if (!getFunnelCookie()) + // createOrUpdateCookie(cookieNames.FUNNEL_SESSION_INFO, {}, {}); // Set up cookie with all the props. setFunnelCookieProperties({ @@ -48,14 +49,14 @@ export function getFunnelCookie() { Removes cookie from browser. */ export function deleteFunnelCookie() { - document.cookie = `${cookieNames.FUNNEL_SESSION_INFO}=; Max-Age=0; path=${applicationConfig.COOKIE_PATH}; ${getCookieDomainValue()}`; + createOrUpdateCookie(cookieNames.FUNNEL_SESSION_INFO, undefined, { maxAge: 0 }); } /* Gets cookie domain value. Localhost will be empty "". */ export function getCookieDomainValue() { - return location.hostname.includes("localhost") ? "" : `domain=${getDomainWithoutSubdomain()};`; + return isLocalhost() ? "" : `domain=${getDomainWithoutSubdomain()};`; } /* @@ -110,13 +111,13 @@ export function getSessionIdValue(){ Updates session ID cookie with new expiration date */ export function updateSessionIdCookie() { - createCookie(cookieNames.SESSION_ID, getSessionIdValue(), { maxAge: 60 * 30 }); + createOrUpdateCookie(cookieNames.SESSION_ID, getSessionIdValue(), { maxAge: 60 * 30 }); } -export function setCookieProperties(properties, { useDefaultFunnelCookieAttributes = true, maxAge }) { +export function setCookieProperties(properties, { useDefaultFunnelCookieAttributes = true, maxAge, isSecure }) { if (typeof properties == "object") { Object.keys(properties).forEach(key => { - createCookie(key, properties[key], { useDefaultFunnelCookieAttributes, maxAge }); + createOrUpdateCookie(key, properties[key], { useDefaultFunnelCookieAttributes, maxAge, isSecure }); }); } } @@ -133,6 +134,7 @@ export function setCookieProperties(properties, { useDefaultFunnelCookieAttribut Takes an object with properties to set. Will overwrite existing properties. */ function setFunnelCookieProperties(properties) { + if (typeof properties == "object") { let cookie = getFunnelCookie(); @@ -141,9 +143,8 @@ function setFunnelCookieProperties(properties) { cookie[key] = properties[key]; }); - const cookieValueJson = JSON.stringify(cookie); - - createCookie(cookieNames.FUNNEL_SESSION_INFO, cookieValueJson, {}); + const cookieValueJson = JSON.stringify(cookie); + createOrUpdateCookie(cookieNames.FUNNEL_SESSION_INFO, cookieValueJson, {}); } } } @@ -152,18 +153,16 @@ function setFunnelCookieProperties(properties) { Used to create a cookie. `useDefaultFunnelCookieAttributes` will set the path and domain to our defaults */ -function createCookie(key, value, { useDefaultFunnelCookieAttributes = true, maxAge, isSecure = true }) { +function createOrUpdateCookie(key, value = "", { useDefaultFunnelCookieAttributes = true, maxAge, isSecure = true }) { let cookieToAdd = `${key}=${value}; `; - console.log(isSecure) - console.log(useDefaultFunnelCookieAttributes) if (useDefaultFunnelCookieAttributes) { cookieToAdd += `path=${applicationConfig.COOKIE_PATH}; ${getCookieDomainValue()} `; } - if (isSecure) { + if (isSecure && !isLocalhost()) { cookieToAdd += `secure; `; } - if (maxAge) { + if (!isNaN(maxAge)) { cookieToAdd += `max-age=${maxAge};`; } @@ -175,7 +174,7 @@ function createCookie(key, value, { useDefaultFunnelCookieAttributes = true, max */ function getDomainWithoutSubdomain() { let url = location.hostname; - if (url.includes("localhost")) { + if (isLocalhost()) { return "localhost"; } @@ -198,4 +197,8 @@ function getCookieValueByName(name) { return parts.pop().split(";").shift(); } return ""; +} + +function isLocalhost() { + return location.hostname.includes("localhost"); } \ No newline at end of file diff --git a/src/helpers/unit-test-helper.js b/src/helpers/unit-test-helper.js index 67a3dc8ae..7e26667e7 100644 --- a/src/helpers/unit-test-helper.js +++ b/src/helpers/unit-test-helper.js @@ -6,7 +6,7 @@ import { fmgPageValues } from "@/router/router-constants/fmgPage-values"; import { cookieNames } from "@/constants/cookie-names"; import { Form } from "vee-validate"; import baseMixin from "@/mixins/base-mixin"; -import { getCookieDomainValue } from "@/helpers/heritage-integration/cookie-helper"; +import { getCookieDomainValue, setCookieProperties } from "@/helpers/heritage-integration/cookie-helper"; import { analyticsPageEvents, GaCategories, GaActions, GaLabels, GaEvents, ValueToLogTypes } from "@/constants/analytics"; import { queryStrings } from "@/constants/query-strings"; import { routerParams } from "@/router/router-constants/router-params"; @@ -106,7 +106,8 @@ export function setupCookies({ funnelCookieValue = "", includeHeritageCookie = t Object.keys(cookies).forEach(key => { const cookieValue = key == cookieNames.FUNNEL_SESSION_INFO ? funnelCookieValue : cookies[key]; if (includeHeritageCookie || key != cookieNames.FUNNEL_SESSION_INFO) - document.cookie = `${key}=${cookieValue}; path=/; ${getCookieDomainValue()}`; + setCookieProperties({ [key]: cookieValue }, { isSecure: false }); + // document.cookie = `${key}=${cookieValue}; path=/; ${getCookieDomainValue()}`; }); } From 8d01c6b1b3c296c4e19368294d831f2ad46bd5ee Mon Sep 17 00:00:00 2001 From: Katie Date: Wed, 17 Aug 2022 11:35:03 -0400 Subject: [PATCH 3/4] CSR-759 Cleanup --- src/helpers/heritage-integration/cookie-helper.js | 4 ---- src/helpers/unit-test-helper.js | 1 - 2 files changed, 5 deletions(-) diff --git a/src/helpers/heritage-integration/cookie-helper.js b/src/helpers/heritage-integration/cookie-helper.js index f6f44e3e4..6f4cf1c3b 100644 --- a/src/helpers/heritage-integration/cookie-helper.js +++ b/src/helpers/heritage-integration/cookie-helper.js @@ -9,10 +9,6 @@ export function updateOrCreateFunnelCookie() { const wasClaimRegistrationDelayed = getFunnelCookie()?.HasDelayedClaimRegistration; const shouldSuppressConceptFunnel = getFunnelCookie()?.SuppressConceptFunnel; - // Create the cookie - // if (!getFunnelCookie()) - // createOrUpdateCookie(cookieNames.FUNNEL_SESSION_INFO, {}, {}); - // Set up cookie with all the props. setFunnelCookieProperties({ LastTouched: new Date().toUTCString(), diff --git a/src/helpers/unit-test-helper.js b/src/helpers/unit-test-helper.js index 7e26667e7..cc00e49d9 100644 --- a/src/helpers/unit-test-helper.js +++ b/src/helpers/unit-test-helper.js @@ -107,7 +107,6 @@ export function setupCookies({ funnelCookieValue = "", includeHeritageCookie = t const cookieValue = key == cookieNames.FUNNEL_SESSION_INFO ? funnelCookieValue : cookies[key]; if (includeHeritageCookie || key != cookieNames.FUNNEL_SESSION_INFO) setCookieProperties({ [key]: cookieValue }, { isSecure: false }); - // document.cookie = `${key}=${cookieValue}; path=/; ${getCookieDomainValue()}`; }); } From 264b189a9a2021f63c94a5f2d1982d5b653e58e0 Mon Sep 17 00:00:00 2001 From: Katie Date: Wed, 17 Aug 2022 11:37:32 -0400 Subject: [PATCH 4/4] CSR-759 Cleanup --- src/helpers/heritage-integration/cookie-helper.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/helpers/heritage-integration/cookie-helper.js b/src/helpers/heritage-integration/cookie-helper.js index 6f4cf1c3b..dc1872e74 100644 --- a/src/helpers/heritage-integration/cookie-helper.js +++ b/src/helpers/heritage-integration/cookie-helper.js @@ -130,7 +130,6 @@ export function setCookieProperties(properties, { useDefaultFunnelCookieAttribut Takes an object with properties to set. Will overwrite existing properties. */ function setFunnelCookieProperties(properties) { - if (typeof properties == "object") { let cookie = getFunnelCookie();