1 import { captureMessage } from '@proton/shared/lib/helpers/sentry';
3 import type { ResumedSessionResult } from '../authentication/persistedSessionHelper';
4 import type { APP_NAMES } from '../constants';
5 import { SECOND } from '../constants';
6 import { createTimeoutError } from '../fetch/ApiError';
7 import { getIsAuthorizedApp, getIsDrawerPostMessage, postMessageFromIframe } from './helpers';
8 import { DRAWER_EVENTS, type SESSION_MESSAGE } from './interfaces';
10 export const resumeSessionDrawerApp = ({
12 localID: parentLocalID,
17 let timeout: ReturnType<typeof setTimeout> | undefined;
19 return new Promise<ResumedSessionResult & { tag: SESSION_MESSAGE['payload']['tag'] }>((resolve, reject) => {
20 const handler = (event: MessageEvent) => {
21 if (!getIsDrawerPostMessage(event)) {
25 if (event.data.type === DRAWER_EVENTS.SESSION) {
26 const { UID, keyPassword, User, localID, clientKey, offlineKey, persistent, trusted, tag } =
28 window.removeEventListener('message', handler);
31 clearTimeout(timeout);
43 LocalID: localID ?? parentLocalID,
48 postMessageFromIframe({ type: DRAWER_EVENTS.READY }, parentApp);
49 window.addEventListener('message', handler);
51 // Resolve the promise if the parent app does not respond
52 timeout = setTimeout(() => {
53 captureMessage('Drawer iframe session timeout', {
57 isAuthorizedApp: getIsAuthorizedApp(parentApp || ''),
58 locationOrigin: window.location.origin,
59 locationHref: window.location.href,
63 reject(createTimeoutError({}));