diff --git a/scripts/commerce.js b/scripts/commerce.js index b52f1a43..96523c77 100644 --- a/scripts/commerce.js +++ b/scripts/commerce.js @@ -26,11 +26,15 @@ export function getAuthorization() { } else { env = 'prod'; } + const tokenInStore = sessionStorage.getItem(`${siteID}_${env}_apiToken`); + const parsedToken = JSON.parse(tokenInStore); 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 (parsedToken && parsedToken?.expiry_time > (new Date().getTime() / 1000)) { + authHeader.append('authentication-token', parsedToken.token); } else if (getCookie(`${siteID}_${env}_apiToken`)) { const apiToken = getCookie(`${siteID}_${env}_apiToken`); authHeader.append('authentication-token', apiToken); diff --git a/scripts/delayed.js b/scripts/delayed.js index bee1f034..4acd21dc 100644 --- a/scripts/delayed.js +++ b/scripts/delayed.js @@ -124,20 +124,19 @@ function sendCoveoEventProduct() { async function getAuthToken() { if (!refresh) { refresh = true; + const siteID = window.DanaherConfig?.siteID; const formData = 'grant_type=anonymous&scope=openid+profile&client_id='; - const authRequest = await fetch(`${baseURL}/token`, { + const authRequest = await fetch(`/content/danaher/services/auth/token?id=${siteID}`, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, 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(`${siteID}_${env}_apiToken`, data.access_token, expiresIn, '/'); - localStorage.setItem(`${siteID}_${env}_refresh-token`, data.refresh_token); + sessionStorage.setItem(`${siteID}_${env}_apiToken`, JSON.stringify(data)); + sessionStorage.setItem(`${siteID}_${env}_refresh-token`, data.refresh_token); } } }