@asgardeo/react 0.5.15 → 0.5.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,7 +16,7 @@
16
16
  * under the License.
17
17
  */
18
18
  import { Context } from 'react';
19
- import { HttpRequestConfig, HttpResponse, Organization, SignInOptions } from '@asgardeo/browser';
19
+ import { HttpRequestConfig, HttpResponse, IdToken, Organization, SignInOptions } from '@asgardeo/browser';
20
20
  import AsgardeoReactClient from '../../AsgardeoReactClient';
21
21
  /**
22
22
  * Props interface of {@link AsgardeoContext}
@@ -87,6 +87,14 @@ export type AsgardeoContextProps = {
87
87
  * signInOptions: { prompt: "login", fidp: "OrganizationSSO" }
88
88
  */
89
89
  signInOptions?: SignInOptions;
90
+ /**
91
+ * Function to retrieve the decoded ID token.
92
+ * This function decodes the ID token and returns its payload.
93
+ * It can be used to access user claims and other information contained in the ID token.
94
+ *
95
+ * @returns A promise that resolves to the decoded ID token payload.
96
+ */
97
+ getDecodedIdToken?: () => Promise<IdToken>;
90
98
  };
91
99
  /**
92
100
  * Context object for managing the Authentication flow builder core context.
package/dist/index.js CHANGED
@@ -32,7 +32,8 @@ var AsgardeoContext = createContext({
32
32
  request: () => null,
33
33
  requestAll: () => null
34
34
  },
35
- signInOptions: {}
35
+ signInOptions: {},
36
+ getDecodedIdToken: null
36
37
  });
37
38
  AsgardeoContext.displayName = "AsgardeoContext";
38
39
  var AsgardeoContext_default = AsgardeoContext;
@@ -555,31 +556,35 @@ var AsgardeoReactClient = class extends AsgardeoBrowserClient {
555
556
  }
556
557
  }
557
558
  async getDecodedIdToken(sessionId) {
558
- return this.asgardeo.getDecodedIdToken(sessionId);
559
+ return this.withLoading(async () => {
560
+ return this.asgardeo.getDecodedIdToken(sessionId);
561
+ });
559
562
  }
560
563
  async getUserProfile(options) {
561
- try {
562
- let baseUrl = options?.baseUrl;
563
- if (!baseUrl) {
564
- const configData = await this.asgardeo.getConfigData();
565
- baseUrl = configData?.baseUrl;
564
+ return this.withLoading(async () => {
565
+ try {
566
+ let baseUrl = options?.baseUrl;
567
+ if (!baseUrl) {
568
+ const configData = await this.asgardeo.getConfigData();
569
+ baseUrl = configData?.baseUrl;
570
+ }
571
+ const profile = await getScim2Me_default({ baseUrl });
572
+ const schemas = await getSchemas_default({ baseUrl });
573
+ const processedSchemas = flattenUserSchema(schemas);
574
+ const output = {
575
+ schemas: processedSchemas,
576
+ flattenedProfile: generateFlattenedUserProfile(profile, processedSchemas),
577
+ profile
578
+ };
579
+ return output;
580
+ } catch (error) {
581
+ return {
582
+ schemas: [],
583
+ flattenedProfile: extractUserClaimsFromIdToken(await this.getDecodedIdToken()),
584
+ profile: extractUserClaimsFromIdToken(await this.getDecodedIdToken())
585
+ };
566
586
  }
567
- const profile = await getScim2Me_default({ baseUrl });
568
- const schemas = await getSchemas_default({ baseUrl });
569
- const processedSchemas = flattenUserSchema(schemas);
570
- const output = {
571
- schemas: processedSchemas,
572
- flattenedProfile: generateFlattenedUserProfile(profile, processedSchemas),
573
- profile
574
- };
575
- return output;
576
- } catch (error) {
577
- return {
578
- schemas: [],
579
- flattenedProfile: extractUserClaimsFromIdToken(await this.getDecodedIdToken()),
580
- profile: extractUserClaimsFromIdToken(await this.getDecodedIdToken())
581
- };
582
- }
587
+ });
583
588
  }
584
589
  async getMyOrganizations(options, sessionId) {
585
590
  try {
@@ -616,12 +621,14 @@ var AsgardeoReactClient = class extends AsgardeoBrowserClient {
616
621
  }
617
622
  }
618
623
  async getCurrentOrganization() {
619
- const idToken = await this.getDecodedIdToken();
620
- return {
621
- orgHandle: idToken?.org_handle,
622
- name: idToken?.org_name,
623
- id: idToken?.org_id
624
- };
624
+ return this.withLoading(async () => {
625
+ const idToken = await this.getDecodedIdToken();
626
+ return {
627
+ orgHandle: idToken?.org_handle,
628
+ name: idToken?.org_name,
629
+ id: idToken?.org_id
630
+ };
631
+ });
625
632
  }
626
633
  async switchOrganization(organization, sessionId) {
627
634
  return this.withLoading(async () => {
@@ -666,8 +673,8 @@ var AsgardeoReactClient = class extends AsgardeoBrowserClient {
666
673
  async isInitialized() {
667
674
  return this.asgardeo.isInitialized();
668
675
  }
669
- isSignedIn() {
670
- return this.asgardeo.isSignedIn();
676
+ async isSignedIn() {
677
+ return await this.asgardeo.isSignedIn();
671
678
  }
672
679
  getConfiguration() {
673
680
  return this.asgardeo.getConfigData();
@@ -729,7 +736,9 @@ var AsgardeoReactClient = class extends AsgardeoBrowserClient {
729
736
  return this.asgardeo.httpRequestAll(requestConfigs);
730
737
  }
731
738
  async getAccessToken(sessionId) {
732
- return this.asgardeo.getAccessToken(sessionId);
739
+ return this.withLoading(async () => {
740
+ return this.asgardeo.getAccessToken(sessionId);
741
+ });
733
742
  }
734
743
  };
735
744
  var AsgardeoReactClient_default = AsgardeoReactClient;
@@ -1372,6 +1381,7 @@ var AsgardeoProvider = ({
1372
1381
  organizationHandle,
1373
1382
  applicationId,
1374
1383
  signInOptions,
1384
+ syncSession,
1375
1385
  ...rest
1376
1386
  }) => {
1377
1387
  const reRenderCheckRef = useRef(false);
@@ -1396,8 +1406,10 @@ var AsgardeoProvider = ({
1396
1406
  signUpUrl,
1397
1407
  signInUrl,
1398
1408
  signInOptions,
1409
+ syncSession,
1399
1410
  ...rest
1400
1411
  });
1412
+ const [isUpdatingSession, setIsUpdatingSession] = useState7(false);
1401
1413
  const [brandingPreference, setBrandingPreference] = useState7(null);
1402
1414
  const [isBrandingLoading, setIsBrandingLoading] = useState7(false);
1403
1415
  const [brandingError, setBrandingError] = useState7(null);
@@ -1422,11 +1434,14 @@ var AsgardeoProvider = ({
1422
1434
  }
1423
1435
  reRenderCheckRef.current = true;
1424
1436
  (async () => {
1425
- if (await asgardeo.isSignedIn()) {
1437
+ const isAlreadySignedIn = await asgardeo.isSignedIn();
1438
+ if (isAlreadySignedIn) {
1426
1439
  await updateSession();
1427
1440
  return;
1428
1441
  }
1429
- if (hasAuthParams(new URL(window.location.href), afterSignInUrl)) {
1442
+ const currentUrl = new URL(window.location.href);
1443
+ const hasAuthParamsResult = hasAuthParams(currentUrl, afterSignInUrl);
1444
+ if (hasAuthParamsResult) {
1430
1445
  try {
1431
1446
  await signIn(
1432
1447
  { callOnlyOnRedirect: true }
@@ -1438,6 +1453,7 @@ var AsgardeoProvider = ({
1438
1453
  if (error && Object.prototype.hasOwnProperty.call(error, "code")) {
1439
1454
  }
1440
1455
  }
1456
+ } else {
1441
1457
  }
1442
1458
  })();
1443
1459
  }, []);
@@ -1455,6 +1471,7 @@ var AsgardeoProvider = ({
1455
1471
  clearInterval(interval);
1456
1472
  }
1457
1473
  }, 1e3);
1474
+ } else {
1458
1475
  }
1459
1476
  } catch (error) {
1460
1477
  setIsSignedInSync(false);
@@ -1478,28 +1495,40 @@ var AsgardeoProvider = ({
1478
1495
  }, [asgardeo]);
1479
1496
  useEffect5(() => {
1480
1497
  const checkLoadingState = () => {
1481
- const loadingState = asgardeo.isLoading();
1482
- setIsLoadingSync(loadingState);
1498
+ if (isUpdatingSession) {
1499
+ return;
1500
+ }
1501
+ setIsLoadingSync(asgardeo.isLoading());
1483
1502
  };
1484
1503
  checkLoadingState();
1485
1504
  const interval = setInterval(checkLoadingState, 100);
1486
1505
  return () => {
1487
1506
  clearInterval(interval);
1488
1507
  };
1489
- }, [asgardeo]);
1508
+ }, [asgardeo, isLoadingSync, isSignedInSync, isUpdatingSession]);
1490
1509
  const updateSession = async () => {
1491
1510
  try {
1511
+ setIsUpdatingSession(true);
1492
1512
  setIsLoadingSync(true);
1493
1513
  let _baseUrl2 = baseUrl;
1494
- if ((await asgardeo.getDecodedIdToken())?.["user_org"]) {
1514
+ const decodedToken = await asgardeo.getDecodedIdToken();
1515
+ if (decodedToken?.["user_org"]) {
1495
1516
  _baseUrl2 = `${(await asgardeo.getConfiguration()).baseUrl}/o`;
1496
1517
  setBaseUrl(_baseUrl2);
1497
1518
  }
1498
- setUser(await asgardeo.getUser({ baseUrl: _baseUrl2 }));
1499
- setUserProfile(await asgardeo.getUserProfile({ baseUrl: _baseUrl2 }));
1500
- setCurrentOrganization(await asgardeo.getCurrentOrganization());
1501
- setMyOrganizations(await asgardeo.getMyOrganizations());
1519
+ const user2 = await asgardeo.getUser({ baseUrl: _baseUrl2 });
1520
+ const userProfile2 = await asgardeo.getUserProfile({ baseUrl: _baseUrl2 });
1521
+ const currentOrganization2 = await asgardeo.getCurrentOrganization();
1522
+ const myOrganizations2 = await asgardeo.getMyOrganizations();
1523
+ setUser(user2);
1524
+ setUserProfile(userProfile2);
1525
+ setCurrentOrganization(currentOrganization2);
1526
+ setMyOrganizations(myOrganizations2);
1527
+ const currentSignInStatus = await asgardeo.isSignedIn();
1528
+ setIsSignedInSync(await asgardeo.isSignedIn());
1529
+ } catch (error) {
1502
1530
  } finally {
1531
+ setIsUpdatingSession(false);
1503
1532
  setIsLoadingSync(asgardeo.isLoading());
1504
1533
  }
1505
1534
  };
@@ -1549,6 +1578,7 @@ var AsgardeoProvider = ({
1549
1578
  ]);
1550
1579
  const signIn = async (...args) => {
1551
1580
  try {
1581
+ setIsUpdatingSession(true);
1552
1582
  setIsLoadingSync(true);
1553
1583
  const response = await asgardeo.signIn(...args);
1554
1584
  if (await asgardeo.isSignedIn()) {
@@ -1558,11 +1588,13 @@ var AsgardeoProvider = ({
1558
1588
  } catch (error) {
1559
1589
  throw new Error(`Error while signing in: ${error}`);
1560
1590
  } finally {
1591
+ setIsUpdatingSession(false);
1561
1592
  setIsLoadingSync(asgardeo.isLoading());
1562
1593
  }
1563
1594
  };
1564
1595
  const signInSilently = async (options) => {
1565
1596
  try {
1597
+ setIsUpdatingSession(true);
1566
1598
  setIsLoadingSync(true);
1567
1599
  const response = await asgardeo.signInSilently(options);
1568
1600
  if (await asgardeo.isSignedIn()) {
@@ -1577,11 +1609,13 @@ var AsgardeoProvider = ({
1577
1609
  "An error occurred while trying to sign in silently."
1578
1610
  );
1579
1611
  } finally {
1612
+ setIsUpdatingSession(false);
1580
1613
  setIsLoadingSync(asgardeo.isLoading());
1581
1614
  }
1582
1615
  };
1583
1616
  const switchOrganization = async (organization) => {
1584
1617
  try {
1618
+ setIsUpdatingSession(true);
1585
1619
  setIsLoadingSync(true);
1586
1620
  await asgardeo.switchOrganization(organization);
1587
1621
  if (await asgardeo.isSignedIn()) {
@@ -1595,6 +1629,7 @@ var AsgardeoProvider = ({
1595
1629
  "An error occurred while switching to the specified organization."
1596
1630
  );
1597
1631
  } finally {
1632
+ setIsUpdatingSession(false);
1598
1633
  setIsLoadingSync(asgardeo.isLoading());
1599
1634
  }
1600
1635
  };
@@ -1634,7 +1669,9 @@ var AsgardeoProvider = ({
1634
1669
  request: asgardeo.request.bind(asgardeo),
1635
1670
  requestAll: asgardeo.requestAll.bind(asgardeo)
1636
1671
  },
1637
- signInOptions
1672
+ signInOptions,
1673
+ getDecodedIdToken: asgardeo.getDecodedIdToken.bind(asgardeo),
1674
+ syncSession
1638
1675
  }),
1639
1676
  [
1640
1677
  applicationId,
@@ -1651,7 +1688,8 @@ var AsgardeoProvider = ({
1651
1688
  signInSilently,
1652
1689
  user,
1653
1690
  asgardeo,
1654
- signInOptions
1691
+ signInOptions,
1692
+ syncSession
1655
1693
  ]
1656
1694
  );
1657
1695
  return /* @__PURE__ */ jsx7(AsgardeoContext_default.Provider, { value, children: /* @__PURE__ */ jsx7(I18nProvider_default, { preferences: preferences?.i18n, children: /* @__PURE__ */ jsx7(