Skip to content

Commit

Permalink
Fixing quote cart issue wit the token storage change - affected by ht…
Browse files Browse the repository at this point in the history
…tp-only cookie
  • Loading branch information
deepakk799 committed Dec 11, 2024
1 parent a3452c5 commit 9b5441f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
4 changes: 4 additions & 0 deletions scripts/commerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 4 additions & 5 deletions scripts/delayed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit 9b5441f

Please sign in to comment.