From fe03ed193eb4933c038c670b3278f17daeb780e5 Mon Sep 17 00:00:00 2001 From: Deepak Kumar Date: Mon, 4 Nov 2024 11:27:49 +0100 Subject: [PATCH 1/2] Fix for the API token naming to follow AEM pages (Quotes/checkout) --- scripts/commerce.js | 16 ++++++++++++++-- scripts/delayed.js | 7 +++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/scripts/commerce.js b/scripts/commerce.js index e32d4e915..b52f1a439 100644 --- a/scripts/commerce.js +++ b/scripts/commerce.js @@ -14,13 +14,25 @@ export function getCommerceBase() { */ export function getAuthorization() { const authHeader = new Headers(); + const siteID = window.DanaherConfig?.siteID; + const hostName = window.location.hostname; + let env; + if (hostName.includes('local')) { + env = 'local'; + } else if (hostName.includes('dev')) { + env = 'dev'; + } else if (hostName.includes('stage')) { + env = 'stage'; + } else { + env = 'prod'; + } if (localStorage.getItem('authToken')) { authHeader.append('Authorization', `Bearer ${localStorage.getItem('authToken')}`); } else if (getCookie('ProfileData')) { const { customer_token: apiToken } = getCookie('ProfileData'); authHeader.append('authentication-token', apiToken); - } else if (getCookie('apiToken')) { - const apiToken = getCookie('apiToken'); + } else if (getCookie(`${siteID}_${env}_apiToken`)) { + const apiToken = getCookie(`${siteID}_${env}_apiToken`); authHeader.append('authentication-token', apiToken); } return authHeader; diff --git a/scripts/delayed.js b/scripts/delayed.js index e5a901c30..79da900b5 100644 --- a/scripts/delayed.js +++ b/scripts/delayed.js @@ -131,10 +131,13 @@ async function getAuthToken() { body: formData, }); if (authRequest.ok) { + const siteID = window.DanaherConfig?.siteID; + const hostName = window.location.hostname; + const env = hostName.includes('local') ? 'local' : hostName.includes('dev') ? 'dev' : hostName.includes('stage') ? 'stage' : 'prod'; const data = await authRequest.json(); const expiresIn = data.expires_in * 1000; - setCookie('apiToken', data.access_token, expiresIn, '/'); - localStorage.setItem('refreshToken', data.refresh_token); + setCookie(`${siteID}_${env}_apiToken`, data.access_token, expiresIn, '/'); + localStorage.setItem(`${siteID}_${env}_refresh-token`, data.refresh_token); } } } From a3cbdb962c26cc7e842001c3e65c415c7772a65f Mon Sep 17 00:00:00 2001 From: deepakk799 Date: Mon, 4 Nov 2024 13:28:07 +0100 Subject: [PATCH 2/2] Fixed lint errors --- blocks/product-children/product-children.js | 2 +- blocks/product-resources/product-resources.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/blocks/product-children/product-children.js b/blocks/product-children/product-children.js index c96ef5d08..18728eb0e 100644 --- a/blocks/product-children/product-children.js +++ b/blocks/product-children/product-children.js @@ -5,7 +5,7 @@ import { getCookie, isOTEnabled, } from '../../scripts/scripts.js'; import { - getProductResponse, getSKU, + getProductResponse, } from '../../scripts/commerce.js'; customElements.define('product-tile', ProductTile); diff --git a/blocks/product-resources/product-resources.js b/blocks/product-resources/product-resources.js index 449832b63..b65898c3c 100644 --- a/blocks/product-resources/product-resources.js +++ b/blocks/product-resources/product-resources.js @@ -4,7 +4,7 @@ import { getCookie, isOTEnabled, } from '../../scripts/scripts.js'; import { - getProductResponse, getSKU, + getProductResponse, } from '../../scripts/commerce.js'; const productResources = `