Skip to content

Commit 870af07

Browse files
authored
Refresh Firebase Token Demo App changes (#9294)
* Refresh Firebase Token Demo App changes. * take IdpConfig id as input * yarn run format * Remove unwanted comments * Remove unwanted comments
1 parent 64a5a86 commit 870af07

File tree

2 files changed

+87
-7
lines changed

2 files changed

+87
-7
lines changed

packages/auth/demo/public/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,7 @@
854854
<div class="tab-pane" id="tab-byo-ciam-content">
855855
<h2>Sign in with your CIAM token</h2>
856856
<div id="firebase-token-status">No CIAM token found. User not logged in.</div>
857+
<input type="text" id="idp-config-id" class="form-control" placeholder="IDP Config ID" />
857858
<input type="text" id="byo-ciam-token"
858859
class="form-control" placeholder="Enter CIAM token" />
859860
<button class="btn btn-block btn-primary"

packages/auth/demo/src/index.js

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,86 @@ async function getActiveUserBlocking() {
149149
}
150150
}
151151

152+
class RefreshIdpTokenResult {
153+
idpConfigId;
154+
idToken;
155+
}
156+
157+
class TokenRefreshHandlerImpl {
158+
/**
159+
* Opens a popup to get a 3P ID token and Config ID from the user.
160+
* @returns {Promise<RefreshIdpTokenResult>} A promise that returns IdpConfigId and 3p IDP Id token.
161+
*/
162+
refreshIdpToken() {
163+
return this.promptForTokenAndConfigId();
164+
}
165+
166+
promptForTokenAndConfigId() {
167+
return new Promise((resolve, reject) => {
168+
let isSubmitted = false;
169+
const modalId = 'third-party-token-modal';
170+
171+
const modalHtml = `
172+
<div class="modal fade" id="${modalId}" tabindex="-1" role="dialog" aria-labelledby="tokenModalLabel">
173+
<div class="modal-dialog" role="document">
174+
<div class="modal-content">
175+
<div class="modal-header">
176+
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
177+
<span aria-hidden="true">&times;</span>
178+
</button>
179+
<h4 class="modal-title" id="tokenModalLabel">Enter 3P Token Details</h4>
180+
</div>
181+
<div class="modal-body">
182+
<p>Please enter the third-party token details:</p>
183+
<div class="form-group">
184+
<label for="idp-config-id-input-field">IDP Config ID</label>
185+
<input type="text" class="form-control" id="idp-config-id-input-field" placeholder="eg: idp.example.com">
186+
</div>
187+
<div class="form-group">
188+
<label for="id-token-input-field">ID Token</label>
189+
<input type="text" class="form-control" id="id-token-input-field" placeholder="Paste ID Token here">
190+
</div>
191+
</div>
192+
<div class="modal-footer">
193+
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
194+
<button type="button" class="btn btn-primary" id="token-submit-btn">Submit</button>
195+
</div>
196+
</div>
197+
</div>
198+
</div>
199+
`;
200+
201+
$('body').append(modalHtml);
202+
const $modal = $(`#${modalId}`);
203+
204+
$modal.find('#token-submit-btn').on('click', () => {
205+
isSubmitted = true;
206+
207+
const configId = $modal.find('#idp-config-id-input-field').val();
208+
const token = $modal.find('#id-token-input-field').val();
209+
210+
$modal.modal('hide'); // Hide the modal
211+
212+
const result = new RefreshIdpTokenResult();
213+
result.idpConfigId = configId;
214+
result.idToken = token;
215+
216+
resolve(result);
217+
});
218+
219+
$modal.on('hidden.bs.modal', () => {
220+
$modal.remove();
221+
222+
if (!isSubmitted) {
223+
reject(new Error('User cancelled token input.'));
224+
}
225+
});
226+
227+
$modal.modal('show');
228+
});
229+
}
230+
}
231+
152232
/**
153233
* Refreshes the current user data in the UI, displaying a user info box if
154234
* a user is signed in, or removing it.
@@ -1509,23 +1589,20 @@ function onFinalizeSignInWithTotpMultiFactor(event) {
15091589
}, onAuthError);
15101590
}
15111591

1512-
async function exchangeCIAMToken(token) {
1513-
const firebaseToken = await exchangeToken(
1514-
regionalAuth,
1515-
(idpConfigId = 'Bar-e2e-idpconfig-002'),
1516-
token
1517-
);
1592+
async function exchangeCIAMToken(idpConfigId, token) {
1593+
const firebaseToken = await exchangeToken(regionalAuth, idpConfigId, token);
15181594
return firebaseToken;
15191595
}
15201596

15211597
function onExchangeToken(event) {
15221598
event.preventDefault();
15231599
const byoCiamInput = document.getElementById('byo-ciam-token');
1600+
const idpConfigId = document.getElementById('idp-config-id');
15241601
const firebaseTokenStatus = document.getElementById('firebase-token-status');
15251602

15261603
firebaseTokenStatus.textContent = 'Exchanging token...';
15271604

1528-
exchangeCIAMToken(byoCiamInput.value)
1605+
exchangeCIAMToken(idpConfigId.value, byoCiamInput.value)
15291606
.then(response => {
15301607
firebaseTokenStatus.textContent = '✅ Firebase token is set: ' + response;
15311608
console.log('Token:', response);
@@ -2092,6 +2169,8 @@ function initApp() {
20922169
popupRedirectResolver: browserPopupRedirectResolver,
20932170
tenantConfig: tenantConfig
20942171
});
2172+
const tokenRefreshHandler = new TokenRefreshHandlerImpl();
2173+
regionalAuth.setTokenRefreshHandler(tokenRefreshHandler);
20952174

20962175
const firebaseTokenStatus = document.getElementById('firebase-token-status');
20972176
setTimeout(async () => {

0 commit comments

Comments
 (0)