@@ -109,16 +109,53 @@ const authSetup = function () {
109109 var token = getCookie ( tcJWTCookie ) ;
110110 if ( ! token || isTokenExpired ( token ) ) {
111111 logger ( 'refreshing token... at: ' , `${ d . getHours ( ) } ::${ d . getMinutes ( ) } ::${ d . getSeconds ( ) } ` ) ;
112- auth0 . getTokenSilently ( ) . then ( function ( token ) {
113- showAuth0Info ( ) ;
114- storeToken ( ) ;
115- } ) . catch ( function ( e ) {
116- logger ( "Error in refreshing token: " , e )
117- if ( e . error && ( ( e . error == "login_required" ) || ( e . error == "timeout" ) ) ) {
118- clearInterval ( callRefreshTokenFun ) ;
112+ try {
113+ let issuerHostname = "" ;
114+ if ( token ) {
115+ let tokenJson = decodeToken ( token ) ;
116+ let issuer = tokenJson . iss ;
117+ issuerHostname = extractHostname ( issuer ) ;
118+ }
119+ if ( domain !== issuerHostname ) {
120+ domain = issuerHostname ;
121+ logger ( "reintialize auth0 for new domain.." , domain ) ;
122+ createAuth0Client ( {
123+ domain : domain ,
124+ client_id : clientId ,
125+ cacheLocation : useLocalStorage
126+ ? 'localstorage'
127+ : 'memory' ,
128+ useRefreshTokens : useRefreshTokens
129+ } ) . then ( function ( newAuth0Obj ) {
130+ auth0 = newAuth0Obj ;
131+ auth0 . getTokenSilently ( ) . then ( function ( token ) {
132+ showAuth0Info ( ) ;
133+ storeToken ( ) ;
134+ logger ( "refreshing token for new domain.." , domain ) ;
135+ } ) . catch ( function ( e ) {
136+ logger ( "Error in refreshing token: " , e )
137+ if ( e . error && ( ( e . error == "login_required" ) || ( e . error == "timeout" ) ) ) {
138+ clearInterval ( callRefreshTokenFun ) ;
139+ }
140+ }
141+ ) ;
142+ } ) ;
143+ } else {
144+ auth0 . getTokenSilently ( ) . then ( function ( token ) {
145+ showAuth0Info ( ) ;
146+ storeToken ( ) ;
147+ } ) . catch ( function ( e ) {
148+ logger ( "Error in refreshing token: " , e )
149+ if ( e . error && ( ( e . error == "login_required" ) || ( e . error == "timeout" ) ) ) {
150+ clearInterval ( callRefreshTokenFun ) ;
151+ }
152+ }
153+ ) ;
119154 }
155+ } catch ( e ) {
156+ logger ( "Error in refresh token function " , e . message )
120157 }
121- ) ;
158+
122159 }
123160 } ;
124161
@@ -374,6 +411,24 @@ const authSetup = function () {
374411 }
375412 }
376413
414+ function extractHostname ( url ) {
415+ var hostname ;
416+ //find & remove protocol (http, ftp, etc.) and get hostname
417+
418+ if ( url . indexOf ( "//" ) > - 1 ) {
419+ hostname = url . split ( '/' ) [ 2 ] ;
420+ }
421+ else {
422+ hostname = url . split ( '/' ) [ 0 ] ;
423+ }
424+ //find & remove port number
425+ hostname = hostname . split ( ':' ) [ 0 ] ;
426+ //find & remove "?"
427+ hostname = hostname . split ( '?' ) [ 0 ] ;
428+
429+ return hostname ;
430+ }
431+
377432 // execute
378433 init ( ) ;
379434} ;
0 commit comments