Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions libs/blocks/email-collection/email-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
runtimePost,
redirectToSignIn,
isUserGuest,
validateImsToken,
FORM_FIELDS,
} from './utils.js';

Expand Down Expand Up @@ -206,6 +207,7 @@ async function decorateInput(key, value) {
}

async function submitForm(form) {
await validateImsToken();
const isGuest = await isUserGuest();
if (signIn && isGuest) {
await redirectToSignIn(dialog);
Expand Down Expand Up @@ -509,6 +511,7 @@ async function checkIsSubscribed() {
}

async function decorate(el, blockChildren) {
await validateImsToken();
const isGuest = await isUserGuest();
if (signIn && isGuest) {
await redirectToSignIn(dialog);
Expand Down
28 changes: 24 additions & 4 deletions libs/blocks/email-collection/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,31 @@ export const FORM_FIELDS = {

export async function getIMS() {
if (window.adobeIMS) return window.adobeIMS;

await loadIms();
try {
await loadIms();
} catch (e) {
return null;
}
return window.adobeIMS;
}

async function refreshIMSToken(ims) {
try {
await ims?.refreshToken();
} catch (e) {
window.lana?.log('Refreshing IMS token failed');
}
}

export async function validateImsToken() {
const ims = await getIMS();
try {
await ims?.validateToken();
} catch (e) {
await refreshIMSToken(ims);
}
}

export async function getIMSAccessToken() {
try {
const ims = await getIMS();
Expand Down Expand Up @@ -330,7 +350,7 @@ function waitForModal() {
export async function redirectToSignIn(dialog) {
const ims = await getIMS();
if (!document.body.contains(dialog)) await waitForModal();
await ims.signIn();
await ims?.signIn();
if (dialog) closeModal(dialog);
}

Expand Down Expand Up @@ -359,5 +379,5 @@ export async function runtimePost(url, data) {

export async function isUserGuest() {
const ims = await getIMS();
return !ims.isSignedInUser();
return !ims?.isSignedInUser();
}
Loading