@@ -50,6 +50,8 @@ export class Auth {
5050
5151 private authProvider : AuthProvider ;
5252
53+ private authProviderPromise : Promise < void > ;
54+
5355 constructor ( options : AuthOptions ) {
5456 if ( ! options . clientId ) throw InitializationError . invalidParams ( "clientId is required" ) ;
5557 if ( ! options . network ) options . network = WEB3AUTH_NETWORK . SAPPHIRE_MAINNET ;
@@ -192,8 +194,9 @@ export class Auth {
192194 if ( this . options . sdkMode === SDK_MODE . IFRAME ) {
193195 this . authProvider = new AuthProvider ( { sdkUrl : this . options . sdkUrl , whiteLabel : this . options . whiteLabel } ) ;
194196 if ( ! this . state . sessionId ) {
195- await this . authProvider . init ( { network : this . options . network , clientId : this . options . clientId } ) ;
197+ this . authProviderPromise = this . authProvider . init ( { network : this . options . network , clientId : this . options . clientId } ) ;
196198 if ( params . nonce ) {
199+ await this . authProviderPromise ;
197200 await this . postLoginInitiatedMessage ( JSON . parse ( params . loginParams ) , params . nonce ) ;
198201 }
199202 }
@@ -227,8 +230,13 @@ export class Auth {
227230
228231 async postLoginInitiatedMessage ( params : LoginParams , nonce ?: string ) : Promise < void > {
229232 if ( this . options . sdkMode !== SDK_MODE . IFRAME ) throw LoginError . invalidLoginParams ( "Cannot perform this action in default mode." ) ;
233+ // This is to ensure that the auth provider is initialized before calling postLoginInitiatedMessage
234+ // This is setup in the init method, if there is no active session.
235+ if ( this . authProviderPromise ) await this . authProviderPromise ;
230236
231- if ( ! this . authProvider || ! this . authProvider . initialized ) {
237+ // if there is an active session, we dont load the auth provider in the init method.
238+ // so we need to initialize it here, if user logged out and then login in again.
239+ if ( ! this . authProvider ?. initialized ) {
232240 await this . authProvider . init ( { network : this . options . network , clientId : this . options . clientId } ) ;
233241 }
234242
@@ -242,7 +250,9 @@ export class Auth {
242250
243251 async postLoginCancelledMessage ( nonce : string ) : Promise < void > {
244252 if ( this . options . sdkMode !== SDK_MODE . IFRAME ) throw LoginError . invalidLoginParams ( "Cannot perform this action in default mode." ) ;
245- if ( ! this . authProvider || ! this . authProvider . initialized ) throw InitializationError . notInitialized ( ) ;
253+ if ( this . authProviderPromise ) await this . authProviderPromise ;
254+
255+ if ( ! this . authProvider ?. initialized ) throw InitializationError . notInitialized ( ) ;
246256
247257 this . authProvider . postLoginCancelledMessage ( nonce ) ;
248258 }
@@ -423,6 +433,10 @@ export class Auth {
423433 return true ;
424434 }
425435
436+ async cleanup ( ) {
437+ if ( this . authProvider ) this . authProvider . cleanup ( ) ;
438+ }
439+
426440 getUserInfo ( ) : AuthUserInfo {
427441 if ( ! this . sessionManager . sessionId ) {
428442 throw LoginError . userNotLoggedIn ( ) ;
0 commit comments