Skip to content

Commit c7f9c60

Browse files
authored
Demo app improvements. (#9299)
* Demo app improvements. * yarn run format * review comments * add unset RegionalAuth button
1 parent 9edb9ac commit c7f9c60

File tree

2 files changed

+137
-26
lines changed

2 files changed

+137
-26
lines changed

packages/auth/demo/public/index.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,27 @@
853853
</div>
854854
<div class="tab-pane" id="tab-byo-ciam-content">
855855
<h2>Sign in with your CIAM token</h2>
856+
857+
<!-- Initialize Regional Auth -->
858+
<div class="group">Initialize Regional Auth</div>
859+
<div class="form-group">
860+
<label for="tenant-id-input">Tenant ID</label>
861+
<input type="text" class="form-control" id="tenant-id-input" placeholder="Enter Tenant ID">
862+
</div>
863+
<button class="btn btn-primary" id="initialize-regional-auth-btn">Initialize Regional Auth</button>
864+
<div id="regional-auth-status"></div>
865+
<button class="btn btn-default" id="clear-regional-auth-id-btn">Clear Regional Auth Instance</button>
866+
<hr>
867+
868+
<!-- Set Token Refresh Handler -->
869+
<div class="group">Set Token Refresh Handler</div>
870+
<button class="btn btn-success" id="set-successful-handler-btn">Set Token Refresh Handler (Success)</button>
871+
<button class="btn btn-danger" id="set-failure-handler-btn">Set Token Refresh Handler (Failure)</button>
872+
<div id="token-handler-status"></div>
873+
<hr>
874+
875+
<!-- Exchange Token -->
876+
<div class="group">Exchange Token</div>
856877
<div id="firebase-token-status">No CIAM token found. User not logged in.</div>
857878
<input type="text" id="idp-config-id" class="form-control" placeholder="IDP Config ID" />
858879
<input type="text" id="byo-ciam-token"

packages/auth/demo/src/index.js

Lines changed: 116 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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,69 @@ 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+
$('#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+
15971660
function onExchangeToken(event) {
15981661
event.preventDefault();
1662+
if (!validateRegionalAuth()) {
1663+
return;
1664+
}
1665+
15991666
const byoCiamInput = document.getElementById('byo-ciam-token');
16001667
const idpConfigId = document.getElementById('idp-config-id');
16011668
const firebaseTokenStatus = document.getElementById('firebase-token-status');
@@ -1610,7 +1677,46 @@ function onExchangeToken(event) {
16101677
.catch(error => {
16111678
(firebaseTokenStatus.textContent = 'Error exchanging token: '), error;
16121679
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.'
16131716
});
1717+
return false;
1718+
}
1719+
return true;
16141720
}
16151721

16161722
/**
@@ -2158,32 +2264,11 @@ function initApp() {
21582264
connectAuthEmulator(auth, AUTH_EMULATOR_URL);
21592265
}
21602266

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+
}
21872272

21882273
tempApp = initializeApp(
21892274
{
@@ -2528,6 +2613,11 @@ function initApp() {
25282613

25292614
// Performs Exchange Token
25302615
$('#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);
25312621
}
25322622

25332623
$(initApp);

0 commit comments

Comments
 (0)