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()}`; }); }