From 2bd510b30c4cc3d81db1b16dd37accb08aa0fe13 Mon Sep 17 00:00:00 2001 From: BaskarMitrah Date: Mon, 30 Mar 2026 13:51:30 +0530 Subject: [PATCH] added fix --- .../blocks/getcredential/getcredential.js | 88 ++++++++++++++----- 1 file changed, 64 insertions(+), 24 deletions(-) diff --git a/hlx_statics/blocks/getcredential/getcredential.js b/hlx_statics/blocks/getcredential/getcredential.js index a892a1a4..f09d06d0 100644 --- a/hlx_statics/blocks/getcredential/getcredential.js +++ b/hlx_statics/blocks/getcredential/getcredential.js @@ -1349,6 +1349,20 @@ function createSignInContent(config) { return signInWrapper; } +const ensureSignedInContextAndOrganizations = async () => { + if (!window.adobeIMS?.isSignedInUser?.()) return; + if (organizationsData && organizationsData.length > 0 && selectedOrganization) return; + try { + const orgs = await fetchOrganizations(); + if (orgs && orgs.length > 0) { + organizationsData = orgs; + await switchOrganization(null); + } + } catch (e) { + // Keep existing fallback org selection when org fetch fails. + } +}; + // ============================================================================ // RETURN PAGE (Previously Created Projects) // ============================================================================ @@ -2109,11 +2123,24 @@ export default async function decorate(block) { }).catch(showFormFromLoading); }; + const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); + + const fetchTemplateEntitlementWithRetry = async (attempts = 4, delayMs = 300) => { + for (let i = 0; i < attempts; i += 1) { + const entitlement = await fetchTemplateEntitlement(); + if (entitlement) return entitlement; + if (i < attempts - 1) { + await sleep(delayMs); + } + } + return null; + }; + // Re-render page after organization change: show loading, then entitlement check (if Request Access) and show correct view. renderPageAfterOrgChange = (sourceContainer) => { setLoadingText(loadingContainer, 'Loading...'); navigateTo(sourceContainer, loadingContainer); - fetchTemplateEntitlement().then(async (entitlement) => { + fetchTemplateEntitlementWithRetry().then(async (entitlement) => { if (requestAccessContainer && entitlement && (entitlement.userEntitled === false || entitlement.orgEntitled === false)) { const leftCardKey = getRequestAccessLeftCardKey(entitlement, selectedOrganization, credentialData.RequestAccess); const profile = await window.adobeIMS?.getProfile().catch(() => null); @@ -2361,6 +2388,7 @@ export default async function decorate(block) { let signInStateWatcherInterval = null; let signInStateWatcherTimeout = null; + let isResolvingSignedInState = false; const stopSignInStateWatcher = () => { if (signInStateWatcherInterval) { clearInterval(signInStateWatcherInterval); @@ -2379,7 +2407,7 @@ export default async function decorate(block) { return; } if (window.adobeIMS?.isSignedInUser?.()) { - checkAlreadySignedIn(); + void checkAlreadySignedIn(); stopSignInStateWatcher(); } }; @@ -2391,7 +2419,7 @@ export default async function decorate(block) { }; // Check if user is already signed in when page loads - const checkAlreadySignedIn = () => { + const checkAlreadySignedIn = async () => { // Don't check if we're handling an OAuth callback const hash = window.location.hash; if (hash && hash.includes('access_token=')) { @@ -2400,38 +2428,50 @@ export default async function decorate(block) { // Check if user is already signed in if (window.adobeIMS && window.adobeIMS.isSignedInUser()) { + if (!signInContainer || signInContainer.classList.contains('hidden')) { + return; + } + if (isResolvingSignedInState) { + return; + } + isResolvingSignedInState = true; + // User is already signed in - hide sign-in page and show appropriate content if (signInContainer && !signInContainer.classList.contains('hidden')) { + try { // Navigate from sign-in to loading setLoadingText(loadingContainer, 'Loading...'); navigateTo(signInContainer, loadingContainer, true); const forceParams = getForceRequestAccessParams(); if (forceParams && requestAccessContainer) { - window.adobeIMS?.getProfile().then((profile) => { - updateRequestAccessLeftColumn(requestAccessContainer, credentialData.RequestAccess, forceParams.edgeCase, { selectedOrganization, userEmail: profile?.email }); - navigateTo(loadingContainer, requestAccessContainer); - }).catch(() => { - updateRequestAccessLeftColumn(requestAccessContainer, credentialData.RequestAccess, forceParams.edgeCase, { selectedOrganization }); - navigateTo(loadingContainer, requestAccessContainer); - }); + const profile = await window.adobeIMS?.getProfile().catch(() => null); + updateRequestAccessLeftColumn(requestAccessContainer, credentialData.RequestAccess, forceParams.edgeCase, { selectedOrganization, userEmail: profile?.email }); + navigateTo(loadingContainer, requestAccessContainer); return; } + + await ensureSignedInContextAndOrganizations(); + // Fetch template data (populates templateData.apis for credential creation) // and check entitlement (same as React: !template.userEntitled || !template.orgEntitled -> RequestAccess) - fetchTemplateEntitlement().then(async (entitlement) => { - if (requestAccessContainer && entitlement && (entitlement.userEntitled === false || entitlement.orgEntitled === false)) { - lastRequestAccessEntitlement = entitlement; - const leftCardKey = getRequestAccessLeftCardKey(entitlement, selectedOrganization, credentialData.RequestAccess); - const profile = await window.adobeIMS?.getProfile().catch(() => null); - updateRequestAccessLeftColumn(requestAccessContainer, credentialData.RequestAccess, leftCardKey, { selectedOrganization, userEmail: profile?.email }); - navigateTo(loadingContainer, requestAccessContainer); - return; - } - runFormOrReturnFlow(); - }).catch(() => runFormOrReturnFlow()); + const entitlement = await fetchTemplateEntitlementWithRetry(); + if (requestAccessContainer && entitlement && (entitlement.userEntitled === false || entitlement.orgEntitled === false)) { + lastRequestAccessEntitlement = entitlement; + const leftCardKey = getRequestAccessLeftCardKey(entitlement, selectedOrganization, credentialData.RequestAccess); + const profile = await window.adobeIMS?.getProfile().catch(() => null); + updateRequestAccessLeftColumn(requestAccessContainer, credentialData.RequestAccess, leftCardKey, { selectedOrganization, userEmail: profile?.email }); + navigateTo(loadingContainer, requestAccessContainer); + return; + } + runFormOrReturnFlow(); + } catch (error) { + runFormOrReturnFlow(); + } finally { + isResolvingSignedInState = false; } } + } }; // Check for IMS callback on page load @@ -2440,18 +2480,18 @@ export default async function decorate(block) { // Check when IMS loads if user is already signed in window.addEventListener('adobeIMS:loaded', () => { handleIMSCallback(); - checkAlreadySignedIn(); + void checkAlreadySignedIn(); startSignInStateWatcher(); }); window.addEventListener('imsReady', () => { handleIMSCallback(); - checkAlreadySignedIn(); + void checkAlreadySignedIn(); startSignInStateWatcher(); }); if (window.adobeIMS && window.adobeIMS.isSignedInUser()) { - checkAlreadySignedIn(); + void checkAlreadySignedIn(); } else { startSignInStateWatcher(); }