Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 64 additions & 24 deletions hlx_statics/blocks/getcredential/getcredential.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
// ============================================================================
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -2379,7 +2407,7 @@ export default async function decorate(block) {
return;
}
if (window.adobeIMS?.isSignedInUser?.()) {
checkAlreadySignedIn();
void checkAlreadySignedIn();
stopSignInStateWatcher();
}
};
Expand All @@ -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=')) {
Expand All @@ -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
Expand All @@ -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();
}
Expand Down
Loading