@@ -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