@@ -229,6 +229,12 @@ class TokenRefreshHandlerImpl {
229
229
}
230
230
}
231
231
232
+ class TokenRefreshHandlerFailureImpl {
233
+ refreshIdpToken ( ) {
234
+ return Promise . reject ( new Error ( 'Simulated failure to refresh IDP token.' ) ) ;
235
+ }
236
+ }
237
+
232
238
/**
233
239
* Refreshes the current user data in the UI, displaying a user info box if
234
240
* a user is signed in, or removing it.
@@ -1594,8 +1600,63 @@ async function exchangeCIAMToken(idpConfigId, token) {
1594
1600
return firebaseToken ;
1595
1601
}
1596
1602
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
+
1597
1649
function onExchangeToken ( event ) {
1598
1650
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
+
1599
1660
const byoCiamInput = document . getElementById ( 'byo-ciam-token' ) ;
1600
1661
const idpConfigId = document . getElementById ( 'idp-config-id' ) ;
1601
1662
const firebaseTokenStatus = document . getElementById ( 'firebase-token-status' ) ;
@@ -1610,9 +1671,40 @@ function onExchangeToken(event) {
1610
1671
. catch ( error => {
1611
1672
( firebaseTokenStatus . textContent = 'Error exchanging token: ' ) , error ;
1612
1673
console . error ( 'Error exchanging token:' , error ) ;
1674
+ onAuthError ( error ) ;
1613
1675
} ) ;
1614
1676
}
1615
1677
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
+
1616
1708
/**
1617
1709
* Adds a new row to insert an OAuth custom parameter key/value pair.
1618
1710
* @param {!jQuery.Event } _event The jQuery event object.
@@ -2158,32 +2250,11 @@ function initApp() {
2158
2250
connectAuthEmulator ( auth , AUTH_EMULATOR_URL ) ;
2159
2251
}
2160
2252
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
+ }
2187
2258
2188
2259
tempApp = initializeApp (
2189
2260
{
@@ -2528,6 +2599,10 @@ function initApp() {
2528
2599
2529
2600
// Performs Exchange Token
2530
2601
$ ( '#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 ) ;
2531
2606
}
2532
2607
2533
2608
$ ( initApp ) ;
0 commit comments