diff --git a/src/helpers/heritage-integration/cookie-helper.js b/src/helpers/heritage-integration/cookie-helper.js index 775a64fec..dc1872e74 100644 --- a/src/helpers/heritage-integration/cookie-helper.js +++ b/src/helpers/heritage-integration/cookie-helper.js @@ -9,9 +9,6 @@ export function updateOrCreateFunnelCookie() { const wasClaimRegistrationDelayed = getFunnelCookie()?.HasDelayedClaimRegistration; const shouldSuppressConceptFunnel = getFunnelCookie()?.SuppressConceptFunnel; - // Create the cookie - document.cookie = `${cookieNames.FUNNEL_SESSION_INFO}={}; path=${applicationConfig.COOKIE_PATH}; ${getCookieDomainValue()};`; - // Set up cookie with all the props. setFunnelCookieProperties({ LastTouched: new Date().toUTCString(), @@ -48,14 +45,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()};`; } /* @@ -106,10 +103,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() { + createOrUpdateCookie(cookieNames.SESSION_ID, getSessionIdValue(), { maxAge: 60 * 30 }); +} + +export function setCookieProperties(properties, { useDefaultFunnelCookieAttributes = true, maxAge, isSecure }) { if (typeof properties == "object") { Object.keys(properties).forEach(key => { - document.cookie = `${key}=${properties[key]}`; + createOrUpdateCookie(key, properties[key], { useDefaultFunnelCookieAttributes, maxAge, isSecure }); }); } } @@ -134,19 +138,38 @@ function setFunnelCookieProperties(properties) { cookie[key] = properties[key]; }); - const cookieValueJson = JSON.stringify(cookie); - - document.cookie = `${cookieNames.FUNNEL_SESSION_INFO}=${cookieValueJson}; path=${applicationConfig.COOKIE_PATH}; ${getCookieDomainValue()}`; + const cookieValueJson = JSON.stringify(cookie); + createOrUpdateCookie(cookieNames.FUNNEL_SESSION_INFO, cookieValueJson, {}); } } } +/* + Used to create a cookie. + `useDefaultFunnelCookieAttributes` will set the path and domain to our defaults +*/ +function createOrUpdateCookie(key, value = "", { useDefaultFunnelCookieAttributes = true, maxAge, isSecure = true }) { + let cookieToAdd = `${key}=${value}; `; + + if (useDefaultFunnelCookieAttributes) { + cookieToAdd += `path=${applicationConfig.COOKIE_PATH}; ${getCookieDomainValue()} `; + } + if (isSecure && !isLocalhost()) { + cookieToAdd += `secure; `; + } + if (!isNaN(maxAge)) { + cookieToAdd += `max-age=${maxAge};`; + } + + document.cookie = cookieToAdd; +} + /* Gets current domain without the subdomain for cookie. */ function getDomainWithoutSubdomain() { let url = location.hostname; - if (url.includes("localhost")) { + if (isLocalhost()) { return "localhost"; } @@ -169,4 +192,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..cc00e49d9 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,7 @@ 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 }); }); } diff --git a/src/mixins/analytics-mixin.js b/src/mixins/analytics-mixin.js index 142131ae1..6eb18e462 100644 --- a/src/mixins/analytics-mixin.js +++ b/src/mixins/analytics-mixin.js @@ -126,10 +126,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 5b5c73d75..46e26baad 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);