Skip to content

Commit b4a1f32

Browse files
fix event waiting for completed and authorized
1 parent 4c289dd commit b4a1f32

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

packages/modal/src/ui/components/Loader/Loader.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ function Loader(props: LoaderProps) {
141141
handleMobileVerifyConnect,
142142
} = props;
143143

144-
145144
const isConnectedAccordingToAuthenticationMode = useMemo(
146145
() =>
147146
(!isConnectAndSignAuthenticationMode && modalStatus === MODAL_STATUS.CONNECTED) ||

packages/no-modal/src/noModal.ts

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -363,22 +363,56 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
363363
this.analytics.track(ANALYTICS_EVENTS.CONNECTION_STARTED, eventData);
364364

365365
return new Promise((resolve, reject) => {
366+
let connectedEventCompleted = false;
367+
let authorizedEventReceived = false;
368+
366369
const cleanup = () => {
367370
this.off(CONNECTOR_EVENTS.CONNECTED, onConnected);
368371
this.off(CONNECTOR_EVENTS.ERRORED, onErrored);
369-
this.off(CONNECTOR_EVENTS.AUTHORIZED, onConnected);
372+
this.off(CONNECTOR_EVENTS.AUTHORIZED, onAuthorized);
373+
};
374+
375+
const checkCompletion = async () => {
376+
// In CONNECT_AND_SIGN mode, wait for both connected event and authorized event
377+
if (finalLoginParams.getIdentityToken) {
378+
if (connectedEventCompleted && authorizedEventReceived) {
379+
await completeConnection();
380+
}
381+
} else {
382+
// In CONNECT_ONLY mode, just wait for connected event
383+
if (connectedEventCompleted) {
384+
await completeConnection();
385+
}
386+
}
387+
};
388+
389+
const completeConnection = async () => {
390+
try {
391+
// track connection completed event
392+
const userInfo = await connector.getUserInfo();
393+
this.analytics.track(ANALYTICS_EVENTS.CONNECTION_COMPLETED, {
394+
...eventData,
395+
is_mfa_enabled: userInfo?.isMfaEnabled,
396+
duration: Date.now() - startTime,
397+
});
398+
cleanup();
399+
resolve(this.provider);
400+
} catch (error) {
401+
cleanup();
402+
reject(error);
403+
}
370404
};
405+
371406
const onConnected = async () => {
372-
// track connection completed event
373-
const userInfo = await connector.getUserInfo();
374-
this.analytics.track(ANALYTICS_EVENTS.CONNECTION_COMPLETED, {
375-
...eventData,
376-
is_mfa_enabled: userInfo?.isMfaEnabled,
377-
duration: Date.now() - startTime,
378-
});
379-
cleanup();
380-
resolve(this.provider);
407+
connectedEventCompleted = true;
408+
await checkCompletion();
381409
};
410+
411+
const onAuthorized = async () => {
412+
authorizedEventReceived = true;
413+
await checkCompletion();
414+
};
415+
382416
const onErrored = async (err: Web3AuthError) => {
383417
// track connection failed event
384418
this.analytics.track(ANALYTICS_EVENTS.CONNECTION_FAILED, {
@@ -390,10 +424,9 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
390424
reject(err);
391425
};
392426

427+
this.once(CONNECTOR_EVENTS.CONNECTED, onConnected);
393428
if (finalLoginParams.getIdentityToken) {
394-
this.once(CONNECTOR_EVENTS.AUTHORIZED, onConnected);
395-
} else {
396-
this.once(CONNECTOR_EVENTS.CONNECTED, onConnected);
429+
this.once(CONNECTOR_EVENTS.AUTHORIZED, onAuthorized);
397430
}
398431
this.once(CONNECTOR_EVENTS.ERRORED, onErrored);
399432
connector.connect(finalLoginParams);

0 commit comments

Comments
 (0)