Skip to content

Commit

Permalink
Merge pull request #1289 from hlxsites/api-token-fix
Browse files Browse the repository at this point in the history
Fix for the API token naming to follow AEM pages (Quotes/checkout)
  • Loading branch information
davenichols-DHLS authored Nov 4, 2024
2 parents dd88ef8 + a3cbdb9 commit 619f828
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion blocks/product-children/product-children.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getCookie, isOTEnabled,
} from '../../scripts/scripts.js';
import {
getProductResponse, getSKU,
getProductResponse,
} from '../../scripts/commerce.js';

customElements.define('product-tile', ProductTile);
Expand Down
2 changes: 1 addition & 1 deletion blocks/product-resources/product-resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
getCookie, isOTEnabled,
} from '../../scripts/scripts.js';
import {
getProductResponse, getSKU,
getProductResponse,
} from '../../scripts/commerce.js';

const productResources = `
Expand Down
16 changes: 14 additions & 2 deletions scripts/commerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions scripts/delayed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit 619f828

Please sign in to comment.