@@ -229,6 +229,12 @@ class TokenRefreshHandlerImpl {
229229 }
230230}
231231
232+ class TokenRefreshHandlerFailureImpl {
233+ refreshIdpToken ( ) {
234+ return Promise . reject ( new Error ( 'Simulated failure to refresh IDP token.' ) ) ;
235+ }
236+ }
237+
232238/**
233239 * Refreshes the current user data in the UI, displaying a user info box if
234240 * a user is signed in, or removing it.
@@ -1594,8 +1600,63 @@ async function exchangeCIAMToken(idpConfigId, token) {
15941600 return firebaseToken ;
15951601}
15961602
1603+ function onInitializeRegionalAuthClick ( ) {
1604+ const tenantId = $ ( '#tenant-id-input' ) . val ( ) ;
1605+ if ( ! tenantId ) {
1606+ alertError ( 'Please enter a Tenant ID.' ) ;
1607+ return ;
1608+ }
1609+
1610+ try {
1611+ const tenantConfig = {
1612+ location : 'global' ,
1613+ tenantId : tenantId
1614+ } ;
1615+
1616+ const regionalApp = initializeApp ( config , `${ auth . name } -rgcip` ) ;
1617+
1618+ regionalAuth = initializeAuth ( regionalApp , {
1619+ persistence : indexedDBLocalPersistence ,
1620+ popupRedirectResolver : browserPopupRedirectResolver ,
1621+ tenantConfig : tenantConfig
1622+ } ) ;
1623+ const handlerType = localStorage . getItem ( 'tokenRefreshHandlerType' ) ;
1624+ if ( handlerType === 'success' ) {
1625+ onSetSuccessfulHandlerClick ( ) ;
1626+ } else if ( handlerType === 'failure' ) {
1627+ onSetFailureHandlerClick ( ) ;
1628+ }
1629+ const firebaseTokenStatus = document . getElementById (
1630+ 'firebase-token-status'
1631+ ) ;
1632+ setTimeout ( async ( ) => {
1633+ const firebaseToken = await regionalAuth . getFirebaseAccessToken ( ) ;
1634+ if ( firebaseToken ) {
1635+ firebaseTokenStatus . textContent =
1636+ '✅ Firebase token is set: ' + firebaseToken ;
1637+ } else {
1638+ firebaseTokenStatus . textContent =
1639+ 'No CIAM token found. User not logged in.' ;
1640+ }
1641+ console . log ( 'firebaseToken after delay: ' , firebaseToken ) ;
1642+ } , 1000 ) ;
1643+ localStorage . setItem ( 'regionalAuthTenantId' , tenantId ) ;
1644+ } catch ( error ) {
1645+ onAuthError ( error ) ;
1646+ }
1647+ }
1648+
15971649function onExchangeToken ( event ) {
15981650 event . preventDefault ( ) ;
1651+ if ( ! regionalAuth ) {
1652+ onAuthError ( {
1653+ code : 'auth-not-initialized' ,
1654+ message :
1655+ 'Regional Auth is not initialized. Please enter a Tenant ID and initialize.'
1656+ } ) ;
1657+ return ;
1658+ }
1659+
15991660 const byoCiamInput = document . getElementById ( 'byo-ciam-token' ) ;
16001661 const idpConfigId = document . getElementById ( 'idp-config-id' ) ;
16011662 const firebaseTokenStatus = document . getElementById ( 'firebase-token-status' ) ;
@@ -1610,9 +1671,40 @@ function onExchangeToken(event) {
16101671 . catch ( error => {
16111672 ( firebaseTokenStatus . textContent = 'Error exchanging token: ' ) , error ;
16121673 console . error ( 'Error exchanging token:' , error ) ;
1674+ onAuthError ( error ) ;
16131675 } ) ;
16141676}
16151677
1678+ function onSetSuccessfulHandlerClick ( ) {
1679+ if ( ! regionalAuth ) {
1680+ onAuthError ( {
1681+ code : 'auth-not-initialized' ,
1682+ message :
1683+ 'Regional Auth is not initialized. Please enter a Tenant ID and initialize.'
1684+ } ) ;
1685+ return ;
1686+ }
1687+
1688+ const tokenRefreshHandler = new TokenRefreshHandlerImpl ( ) ;
1689+ regionalAuth . setTokenRefreshHandler ( tokenRefreshHandler ) ;
1690+ localStorage . setItem ( 'tokenRefreshHandlerType' , 'success' ) ;
1691+ }
1692+
1693+ function onSetFailureHandlerClick ( ) {
1694+ if ( ! regionalAuth ) {
1695+ onAuthError ( {
1696+ code : 'auth-not-initialized' ,
1697+ message :
1698+ 'Regional Auth is not initialized. Please enter a Tenant ID and initialize.'
1699+ } ) ;
1700+ return ;
1701+ }
1702+
1703+ const tokenRefreshHandler = new TokenRefreshHandlerFailureImpl ( ) ;
1704+ regionalAuth . setTokenRefreshHandler ( tokenRefreshHandler ) ;
1705+ localStorage . setItem ( 'tokenRefreshHandlerType' , 'failure' ) ;
1706+ }
1707+
16161708/**
16171709 * Adds a new row to insert an OAuth custom parameter key/value pair.
16181710 * @param {!jQuery.Event } _event The jQuery event object.
@@ -2158,32 +2250,11 @@ function initApp() {
21582250 connectAuthEmulator ( auth , AUTH_EMULATOR_URL ) ;
21592251 }
21602252
2161- let tenantConfig = {
2162- 'location' : 'global' ,
2163- 'tenantId' : 'Foo-e2e-tenant-001'
2164- } ;
2165- const regionalApp = initializeApp ( config , `${ auth . name } -rgcip` ) ;
2166-
2167- regionalAuth = initializeAuth ( regionalApp , {
2168- persistence : indexedDBLocalPersistence ,
2169- popupRedirectResolver : browserPopupRedirectResolver ,
2170- tenantConfig : tenantConfig
2171- } ) ;
2172- const tokenRefreshHandler = new TokenRefreshHandlerImpl ( ) ;
2173- regionalAuth . setTokenRefreshHandler ( tokenRefreshHandler ) ;
2174-
2175- const firebaseTokenStatus = document . getElementById ( 'firebase-token-status' ) ;
2176- setTimeout ( async ( ) => {
2177- const firebaseToken = await regionalAuth . getFirebaseAccessToken ( ) ;
2178- if ( firebaseToken ) {
2179- firebaseTokenStatus . textContent =
2180- '✅ Firebase token is set: ' + firebaseToken ;
2181- } else {
2182- firebaseTokenStatus . textContent =
2183- 'No CIAM token found. User not logged in.' ;
2184- }
2185- console . log ( 'firebaseToken after delay: ' , firebaseToken ) ;
2186- } , 1000 ) ;
2253+ const persistedTenantId = localStorage . getItem ( 'regionalAuthTenantId' ) ;
2254+ if ( persistedTenantId ) {
2255+ $ ( '#tenant-id-input' ) . val ( persistedTenantId ) ;
2256+ onInitializeRegionalAuthClick ( ) ;
2257+ }
21872258
21882259 tempApp = initializeApp (
21892260 {
@@ -2528,6 +2599,10 @@ function initApp() {
25282599
25292600 // Performs Exchange Token
25302601 $ ( '#exchange-token' ) . click ( onExchangeToken ) ;
2602+
2603+ $ ( '#initialize-regional-auth-btn' ) . click ( onInitializeRegionalAuthClick ) ;
2604+ $ ( '#set-successful-handler-btn' ) . click ( onSetSuccessfulHandlerClick ) ;
2605+ $ ( '#set-failure-handler-btn' ) . click ( onSetFailureHandlerClick ) ;
25312606}
25322607
25332608$ ( initApp ) ;
0 commit comments