@@ -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,69 @@ 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
+ $ ( '#regional-auth-status' ) . text ( '✅ regionalAuth is initialized' ) ;
1624
+ $ ( '#token-handler-status' ) . text ( 'Token refresh handler is not set.' ) ;
1625
+ const handlerType = localStorage . getItem ( 'tokenRefreshHandlerType' ) ;
1626
+ if ( handlerType === 'success' ) {
1627
+ onSetSuccessfulHandlerClick ( ) ;
1628
+ } else if ( handlerType === 'failure' ) {
1629
+ onSetFailureHandlerClick ( ) ;
1630
+ }
1631
+ const firebaseTokenStatus = document . getElementById (
1632
+ 'firebase-token-status'
1633
+ ) ;
1634
+ setTimeout ( async ( ) => {
1635
+ const firebaseToken = await regionalAuth . getFirebaseAccessToken ( ) ;
1636
+ if ( firebaseToken ) {
1637
+ firebaseTokenStatus . textContent =
1638
+ '✅ Firebase token is set: ' + firebaseToken ;
1639
+ } else {
1640
+ firebaseTokenStatus . textContent =
1641
+ 'No CIAM token found. User not logged in.' ;
1642
+ }
1643
+ console . log ( 'firebaseToken after delay: ' , firebaseToken ) ;
1644
+ } , 1000 ) ;
1645
+ localStorage . setItem ( 'regionalAuthTenantId' , tenantId ) ;
1646
+ } catch ( error ) {
1647
+ onAuthError ( error ) ;
1648
+ }
1649
+ }
1650
+
1651
+ function clearRegionalAuthInstance ( ) {
1652
+ localStorage . removeItem ( 'regionalAuthTenantId' ) ;
1653
+ localStorage . removeItem ( 'tokenRefreshHandlerType' ) ; // Clear handler choice
1654
+ $ ( '#tenant-id-input' ) . val ( '' ) ;
1655
+ regionalAuth = null ;
1656
+ $ ( '#regional-auth-status' ) . text ( 'regionalAuth is not initialized' ) ;
1657
+ $ ( '#token-handler-status' ) . text ( 'Token refresh handler is not set.' ) ;
1658
+ }
1659
+
1597
1660
function onExchangeToken ( event ) {
1598
1661
event . preventDefault ( ) ;
1662
+ if ( ! validateRegionalAuth ( ) ) {
1663
+ return ;
1664
+ }
1665
+
1599
1666
const byoCiamInput = document . getElementById ( 'byo-ciam-token' ) ;
1600
1667
const idpConfigId = document . getElementById ( 'idp-config-id' ) ;
1601
1668
const firebaseTokenStatus = document . getElementById ( 'firebase-token-status' ) ;
@@ -1610,7 +1677,46 @@ function onExchangeToken(event) {
1610
1677
. catch ( error => {
1611
1678
( firebaseTokenStatus . textContent = 'Error exchanging token: ' ) , error ;
1612
1679
console . error ( 'Error exchanging token:' , error ) ;
1680
+ onAuthError ( error ) ;
1681
+ } ) ;
1682
+ }
1683
+
1684
+ function onSetSuccessfulHandlerClick ( ) {
1685
+ if ( ! validateRegionalAuth ( ) ) {
1686
+ return ;
1687
+ }
1688
+
1689
+ const tokenRefreshHandler = new TokenRefreshHandlerImpl ( ) ;
1690
+ regionalAuth . setTokenRefreshHandler ( tokenRefreshHandler ) ;
1691
+ localStorage . setItem ( 'tokenRefreshHandlerType' , 'success' ) ;
1692
+ $ ( '#token-handler-status' ) . text (
1693
+ '✅ Token refresh handler is set to success.'
1694
+ ) ;
1695
+ }
1696
+
1697
+ function onSetFailureHandlerClick ( ) {
1698
+ if ( ! validateRegionalAuth ( ) ) {
1699
+ return ;
1700
+ }
1701
+
1702
+ const tokenRefreshHandler = new TokenRefreshHandlerFailureImpl ( ) ;
1703
+ regionalAuth . setTokenRefreshHandler ( tokenRefreshHandler ) ;
1704
+ localStorage . setItem ( 'tokenRefreshHandlerType' , 'failure' ) ;
1705
+ $ ( '#token-handler-status' ) . text (
1706
+ '✅ Token refresh handler is set to failure.'
1707
+ ) ;
1708
+ }
1709
+
1710
+ function validateRegionalAuth ( ) {
1711
+ if ( ! regionalAuth ) {
1712
+ onAuthError ( {
1713
+ code : 'auth-not-initialized' ,
1714
+ message :
1715
+ 'Regional Auth is not initialized. Please enter a Tenant ID and initialize.'
1613
1716
} ) ;
1717
+ return false ;
1718
+ }
1719
+ return true ;
1614
1720
}
1615
1721
1616
1722
/**
@@ -2158,32 +2264,11 @@ function initApp() {
2158
2264
connectAuthEmulator ( auth , AUTH_EMULATOR_URL ) ;
2159
2265
}
2160
2266
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 ) ;
2267
+ const persistedTenantId = localStorage . getItem ( 'regionalAuthTenantId' ) ;
2268
+ if ( persistedTenantId ) {
2269
+ $ ( '#tenant-id-input' ) . val ( persistedTenantId ) ;
2270
+ onInitializeRegionalAuthClick ( ) ;
2271
+ }
2187
2272
2188
2273
tempApp = initializeApp (
2189
2274
{
@@ -2528,6 +2613,11 @@ function initApp() {
2528
2613
2529
2614
// Performs Exchange Token
2530
2615
$ ( '#exchange-token' ) . click ( onExchangeToken ) ;
2616
+
2617
+ $ ( '#initialize-regional-auth-btn' ) . click ( onInitializeRegionalAuthClick ) ;
2618
+ $ ( '#set-successful-handler-btn' ) . click ( onSetSuccessfulHandlerClick ) ;
2619
+ $ ( '#set-failure-handler-btn' ) . click ( onSetFailureHandlerClick ) ;
2620
+ $ ( '#clear-regional-auth-id-btn' ) . click ( clearRegionalAuthInstance ) ;
2531
2621
}
2532
2622
2533
2623
$ ( initApp ) ;
0 commit comments