@@ -80,7 +80,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
8080
8181 private tkey : TKeyTSS | null = null ;
8282
83- private sessionManager ! : SessionManager < SessionData > ;
83+ private sessionManager ? : SessionManager < SessionData > ;
8484
8585 private currentStorage : AsyncStorage ;
8686
@@ -108,27 +108,33 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
108108
109109 const isNodejsOrRN = this . isNodejsOrRN ( options . uxMode ) ;
110110
111- // if (await storageAvailable(options.storage)) {
112- // throw CoreKitError.storageTypeUnsupported(`Unsupported storage type ${options.storageKey} for ${options.uxMode} mode.`);
113- // }
114-
115111 if ( options . enableLogging ) {
116112 log . enableAll ( ) ;
117113 this . enableLogging = true ;
118114 } else log . setLevel ( "error" ) ;
119115 if ( typeof options . manualSync !== "boolean" ) options . manualSync = false ;
120116 if ( ! options . web3AuthNetwork ) options . web3AuthNetwork = WEB3AUTH_NETWORK . MAINNET ;
117+ // if sessionTime is not provided, it is defaulted to 86400
121118 if ( ! options . sessionTime ) options . sessionTime = 86400 ;
122119 if ( ! options . serverTimeOffset ) options . serverTimeOffset = 0 ;
123120 if ( ! options . uxMode ) options . uxMode = UX_MODE . REDIRECT ;
124121 if ( ! options . redirectPathName ) options . redirectPathName = "redirect" ;
125122 if ( ! options . baseUrl ) options . baseUrl = isNodejsOrRN ? "https://localhost" : `${ window ?. location . origin } /serviceworker` ;
126123 if ( ! options . disableHashedFactorKey ) options . disableHashedFactorKey = false ;
127124 if ( ! options . hashedFactorNonce ) options . hashedFactorNonce = options . web3AuthClientId ;
125+ if ( options . disableSessionManager === undefined ) options . disableSessionManager = false ;
128126
129127 this . options = options as Web3AuthOptionsWithDefaults ;
130128
131129 this . currentStorage = new AsyncStorage ( this . _storageBaseKey , options . storage ) ;
130+
131+ if ( ! options . disableSessionManager ) {
132+ this . sessionManager = new SessionManager < SessionData > ( {
133+ sessionTime : options . sessionTime ,
134+ } ) ;
135+ }
136+
137+ TorusUtils . setSessionTime ( this . options . sessionTime ) ;
132138 }
133139
134140 get tKey ( ) : TKeyTSS {
@@ -270,13 +276,6 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
270276
271277 this . ready = true ;
272278
273- // setup session Manager during init instead of async constructor
274- const sessionId = await this . currentStorage . get < string > ( "sessionId" ) ;
275- this . sessionManager = new SessionManager ( {
276- sessionTime : this . options . sessionTime ,
277- sessionId,
278- } ) ;
279-
280279 // try handle redirect flow if enabled and return(redirect) from oauth login
281280 if (
282281 params . handleRedirectResult &&
@@ -286,26 +285,31 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
286285 // on failed redirect, instance is reseted.
287286 // skip check feature gating on redirection as it was check before login
288287 await this . handleRedirectResult ( ) ;
288+
289+ // return early on successful redirect, the rest of the code will not be executed
290+ return ;
291+ } else if ( params . rehydrate && this . sessionManager ) {
289292 // if not redirect flow try to rehydrate session if available
290- } else if ( params . rehydrate && this . sessionManager . sessionId ) {
291- // swallowed, should not throw on rehydrate timed out session
292- const sessionResult = await this . sessionManager . authorizeSession ( ) . catch ( async ( err ) => {
293- log . error ( "rehydrate session error" , err ) ;
294- } ) ;
293+ const sessionId = await this . currentStorage . get < string > ( "sessionId" ) ;
294+ if ( sessionId ) {
295+ this . sessionManager . sessionId = sessionId ;
295296
296- // try rehydrate session
297- if ( sessionResult ) {
298- await this . rehydrateSession ( sessionResult ) ;
299- } else {
300- // feature gating on no session rehydration
301- await this . featureRequest ( ) ;
302- TorusUtils . setSessionTime ( this . options . sessionTime ) ;
297+ // swallowed, should not throw on rehydrate timed out session
298+ const sessionResult = await this . sessionManager . authorizeSession ( ) . catch ( async ( err ) => {
299+ log . error ( "rehydrate session error" , err ) ;
300+ } ) ;
301+
302+ // try rehydrate session
303+ if ( sessionResult ) {
304+ await this . rehydrateSession ( sessionResult ) ;
305+
306+ // return early on success rehydration
307+ return ;
308+ }
303309 }
304- } else {
305- // feature gating if not redirect flow or session rehydration
306- await this . featureRequest ( ) ;
307- TorusUtils . setSessionTime ( this . options . sessionTime ) ;
308310 }
311+ // feature gating if not redirect flow or session rehydration
312+ await this . featureRequest ( ) ;
309313 }
310314
311315 public async loginWithOAuth ( params : OAuthLoginParams ) : Promise < void > {
@@ -697,7 +701,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
697701 }
698702
699703 public async logout ( ) : Promise < void > {
700- if ( this . sessionManager . sessionId ) {
704+ if ( this . sessionManager ? .sessionId ) {
701705 await this . sessionManager . invalidateSession ( ) ;
702706 }
703707 // to accommodate async storage
@@ -1034,11 +1038,15 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
10341038 userInfo : result . userInfo ,
10351039 } ) ;
10361040 } catch ( err ) {
1037- log . error ( "error trying to authorize session", err ) ;
1041+ log . warn ( "failed to authorize session", err ) ;
10381042 }
10391043 }
10401044
10411045 private async createSession ( ) {
1046+ if ( ! this . sessionManager ) {
1047+ throw new Error ( "sessionManager is not available" ) ;
1048+ }
1049+
10421050 try {
10431051 const sessionId = SessionManager . generateRandomSessionKey ( ) ;
10441052 this . sessionManager . sessionId = sessionId ;
0 commit comments