@asgardeo/react 0.5.7 → 0.5.9

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,8 @@
16
16
  * under the License.
17
17
  */
18
18
  import { Context } from 'react';
19
- import { Organization } from '@asgardeo/browser';
19
+ import { HttpRequestConfig, HttpResponse, Organization } from '@asgardeo/browser';
20
+ import AsgardeoReactClient from '../../AsgardeoReactClient';
20
21
  /**
21
22
  * Props interface of {@link AsgardeoContext}
22
23
  */
@@ -42,6 +43,7 @@ export type AsgardeoContextProps = {
42
43
  * TODO: Fix the types.
43
44
  */
44
45
  signIn: any;
46
+ signInSilently: AsgardeoReactClient['signInSilently'];
45
47
  /**
46
48
  * Sign-out function to terminate the authentication session.
47
49
  * @remark This is the programmatic version of the `SignOutButton` component.
@@ -56,6 +58,13 @@ export type AsgardeoContextProps = {
56
58
  signUp: any;
57
59
  user: any;
58
60
  organization: Organization;
61
+ /**
62
+ * Custom fetch function to make HTTP requests.
63
+ * @param url - The URL to fetch.
64
+ * @param options - Optional configuration for the HTTP request.
65
+ * @returns A promise that resolves to the HTTP response.
66
+ */
67
+ fetch: (url: string, options?: HttpRequestConfig) => Promise<HttpResponse<any>>;
59
68
  };
60
69
  /**
61
70
  * Context object for managing the Authentication flow builder core context.
package/dist/index.d.ts CHANGED
@@ -193,3 +193,4 @@ export { default as getSchemas, GetSchemasConfig } from './api/getSchemas';
193
193
  export { default as updateMeProfile, UpdateMeProfileConfig } from './api/updateMeProfile';
194
194
  export { default as getMeProfile } from './api/getScim2Me';
195
195
  export * from './api/getScim2Me';
196
+ export { AsgardeoRuntimeError } from '@asgardeo/browser';
package/dist/index.js CHANGED
@@ -24,9 +24,11 @@ var AsgardeoContext = createContext({
24
24
  isSignedIn: false,
25
25
  organization: null,
26
26
  signIn: null,
27
+ signInSilently: null,
27
28
  signOut: null,
28
29
  signUp: null,
29
- user: null
30
+ user: null,
31
+ fetch: () => null
30
32
  });
31
33
  AsgardeoContext.displayName = "AsgardeoContext";
32
34
  var AsgardeoContext_default = AsgardeoContext;
@@ -356,29 +358,14 @@ var _AuthAPI = class _AuthAPI {
356
358
  *
357
359
  * @example
358
360
  *```
359
- * client.trySignInSilently()
361
+ * client.signInSilently()
360
362
  *```
361
363
  */
362
- async trySignInSilently(state, dispatch, additionalParams, tokenRequestConfig) {
363
- return this._client.trySignInSilently(additionalParams, tokenRequestConfig).then(async (response) => {
364
+ async signInSilently(additionalParams, tokenRequestConfig) {
365
+ return this._client.signInSilently(additionalParams, tokenRequestConfig).then(async (response) => {
364
366
  if (!response) {
365
- this.updateState({ ...this.getState(), isLoading: false });
366
- dispatch({ ...state, isLoading: false });
367
367
  return false;
368
368
  }
369
- if (await this._client.isSignedIn()) {
370
- const basicUserInfo = response;
371
- const stateToUpdate = {
372
- displayName: basicUserInfo.displayName,
373
- email: basicUserInfo.email,
374
- isSignedIn: true,
375
- isLoading: false,
376
- isSigningOut: false,
377
- username: basicUserInfo.username
378
- };
379
- this.updateState(stateToUpdate);
380
- dispatch({ ...state, ...stateToUpdate });
381
- }
382
369
  return response;
383
370
  }).catch((error) => Promise.reject(error));
384
371
  }
@@ -514,14 +501,38 @@ var AsgardeoReactClient = class extends AsgardeoBrowserClient {
514
501
  constructor() {
515
502
  super();
516
503
  __publicField(this, "asgardeo");
504
+ __publicField(this, "_isLoading", false);
517
505
  this.asgardeo = new api_default();
518
506
  }
507
+ /**
508
+ * Set the loading state of the client
509
+ * @param loading - Boolean indicating if the client is in a loading state
510
+ */
511
+ setLoading(loading) {
512
+ this._isLoading = loading;
513
+ }
514
+ /**
515
+ * Wrap async operations with loading state management
516
+ * @param operation - The async operation to execute
517
+ * @returns Promise with the result of the operation
518
+ */
519
+ async withLoading(operation) {
520
+ this.setLoading(true);
521
+ try {
522
+ const result = await operation();
523
+ return result;
524
+ } finally {
525
+ this.setLoading(false);
526
+ }
527
+ }
519
528
  initialize(config) {
520
529
  let resolvedOrganizationHandle = config?.organizationHandle;
521
530
  if (!resolvedOrganizationHandle) {
522
531
  resolvedOrganizationHandle = deriveOrganizationHandleFromBaseUrl(config?.baseUrl);
523
532
  }
524
- return this.asgardeo.init({ ...config, organizationHandle: resolvedOrganizationHandle });
533
+ return this.withLoading(async () => {
534
+ return this.asgardeo.init({ ...config, organizationHandle: resolvedOrganizationHandle });
535
+ });
525
536
  }
526
537
  async updateUserProfile(payload, userId) {
527
538
  throw new Error("Not implemented");
@@ -610,47 +621,49 @@ var AsgardeoReactClient = class extends AsgardeoBrowserClient {
610
621
  };
611
622
  }
612
623
  async switchOrganization(organization, sessionId) {
613
- try {
614
- const configData = await this.asgardeo.getConfigData();
615
- const scopes = configData?.scopes;
616
- if (!organization.id) {
624
+ return this.withLoading(async () => {
625
+ try {
626
+ const configData = await this.asgardeo.getConfigData();
627
+ const scopes = configData?.scopes;
628
+ if (!organization.id) {
629
+ throw new AsgardeoRuntimeError(
630
+ "Organization ID is required for switching organizations",
631
+ "react-AsgardeoReactClient-SwitchOrganizationError-001",
632
+ "react",
633
+ "The organization object must contain a valid ID to perform the organization switch."
634
+ );
635
+ }
636
+ const exchangeConfig = {
637
+ attachToken: false,
638
+ data: {
639
+ client_id: "{{clientId}}",
640
+ grant_type: "organization_switch",
641
+ scope: "{{scopes}}",
642
+ switching_organization: organization.id,
643
+ token: "{{accessToken}}"
644
+ },
645
+ id: "organization-switch",
646
+ returnsSession: true,
647
+ signInRequired: true
648
+ };
649
+ return await this.asgardeo.exchangeToken(
650
+ exchangeConfig,
651
+ (user) => {
652
+ },
653
+ () => null
654
+ );
655
+ } catch (error) {
617
656
  throw new AsgardeoRuntimeError(
618
- "Organization ID is required for switching organizations",
619
- "react-AsgardeoReactClient-SwitchOrganizationError-001",
657
+ `Failed to switch organization: ${error.message || error}`,
658
+ "react-AsgardeoReactClient-SwitchOrganizationError-003",
620
659
  "react",
621
- "The organization object must contain a valid ID to perform the organization switch."
660
+ "An error occurred while switching to the specified organization. Please try again."
622
661
  );
623
662
  }
624
- const exchangeConfig = {
625
- attachToken: false,
626
- data: {
627
- client_id: "{{clientId}}",
628
- grant_type: "organization_switch",
629
- scope: "{{scopes}}",
630
- switching_organization: organization.id,
631
- token: "{{accessToken}}"
632
- },
633
- id: "organization-switch",
634
- returnsSession: true,
635
- signInRequired: true
636
- };
637
- return await this.asgardeo.exchangeToken(
638
- exchangeConfig,
639
- (user) => {
640
- },
641
- () => null
642
- );
643
- } catch (error) {
644
- throw new AsgardeoRuntimeError(
645
- `Failed to switch organization: ${error.message || error}`,
646
- "react-AsgardeoReactClient-SwitchOrganizationError-003",
647
- "react",
648
- "An error occurred while switching to the specified organization. Please try again."
649
- );
650
- }
663
+ });
651
664
  }
652
665
  isLoading() {
653
- return this.asgardeo.isLoading();
666
+ return this._isLoading || this.asgardeo.isLoading();
654
667
  }
655
668
  async isInitialized() {
656
669
  return this.asgardeo.isInitialized();
@@ -662,15 +675,22 @@ var AsgardeoReactClient = class extends AsgardeoBrowserClient {
662
675
  return this.asgardeo.getConfigData();
663
676
  }
664
677
  async signIn(...args) {
665
- const arg1 = args[0];
666
- const arg2 = args[1];
667
- if (typeof arg1 === "object" && "flowId" in arg1 && typeof arg2 === "object" && "url" in arg2) {
668
- return executeEmbeddedSignInFlow({
669
- payload: arg1,
670
- url: arg2.url
671
- });
672
- }
673
- return await this.asgardeo.signIn(arg1);
678
+ return this.withLoading(async () => {
679
+ const arg1 = args[0];
680
+ const arg2 = args[1];
681
+ if (typeof arg1 === "object" && "flowId" in arg1 && typeof arg2 === "object" && "url" in arg2) {
682
+ return executeEmbeddedSignInFlow({
683
+ payload: arg1,
684
+ url: arg2.url
685
+ });
686
+ }
687
+ return await this.asgardeo.signIn(arg1);
688
+ });
689
+ }
690
+ async signInSilently(options) {
691
+ return this.withLoading(async () => {
692
+ return this.asgardeo.signInSilently(options);
693
+ });
674
694
  }
675
695
  async signOut(...args) {
676
696
  if (args[1] && typeof args[1] !== "function") {
@@ -704,6 +724,12 @@ var AsgardeoReactClient = class extends AsgardeoBrowserClient {
704
724
  "The signUp method with SignUpOptions is not implemented in the React client."
705
725
  );
706
726
  }
727
+ async fetch(url, options) {
728
+ return this.asgardeo.httpRequest({
729
+ url,
730
+ ...options
731
+ });
732
+ }
707
733
  };
708
734
  var AsgardeoReactClient_default = AsgardeoReactClient;
709
735
 
@@ -1348,6 +1374,7 @@ var AsgardeoProvider = ({
1348
1374
  const [currentOrganization, setCurrentOrganization] = useState7(null);
1349
1375
  const [isSignedInSync, setIsSignedInSync] = useState7(false);
1350
1376
  const [isInitializedSync, setIsInitializedSync] = useState7(false);
1377
+ const [isLoadingSync, setIsLoadingSync] = useState7(true);
1351
1378
  const [myOrganizations, setMyOrganizations] = useState7([]);
1352
1379
  const [userProfile, setUserProfile] = useState7(null);
1353
1380
  const [baseUrl, setBaseUrl] = useState7(_baseUrl);
@@ -1441,16 +1468,32 @@ var AsgardeoProvider = ({
1441
1468
  }
1442
1469
  })();
1443
1470
  }, [asgardeo]);
1471
+ useEffect5(() => {
1472
+ const checkLoadingState = () => {
1473
+ const loadingState = asgardeo.isLoading();
1474
+ setIsLoadingSync(loadingState);
1475
+ };
1476
+ checkLoadingState();
1477
+ const interval = setInterval(checkLoadingState, 100);
1478
+ return () => {
1479
+ clearInterval(interval);
1480
+ };
1481
+ }, [asgardeo]);
1444
1482
  const updateSession = async () => {
1445
- let _baseUrl2 = baseUrl;
1446
- if ((await asgardeo.getDecodedIdToken())?.["user_org"]) {
1447
- _baseUrl2 = `${(await asgardeo.getConfiguration()).baseUrl}/o`;
1448
- setBaseUrl(_baseUrl2);
1449
- }
1450
- setUser(await asgardeo.getUser({ baseUrl: _baseUrl2 }));
1451
- setUserProfile(await asgardeo.getUserProfile({ baseUrl: _baseUrl2 }));
1452
- setCurrentOrganization(await asgardeo.getCurrentOrganization());
1453
- setMyOrganizations(await asgardeo.getMyOrganizations());
1483
+ try {
1484
+ setIsLoadingSync(true);
1485
+ let _baseUrl2 = baseUrl;
1486
+ if ((await asgardeo.getDecodedIdToken())?.["user_org"]) {
1487
+ _baseUrl2 = `${(await asgardeo.getConfiguration()).baseUrl}/o`;
1488
+ setBaseUrl(_baseUrl2);
1489
+ }
1490
+ setUser(await asgardeo.getUser({ baseUrl: _baseUrl2 }));
1491
+ setUserProfile(await asgardeo.getUserProfile({ baseUrl: _baseUrl2 }));
1492
+ setCurrentOrganization(await asgardeo.getCurrentOrganization());
1493
+ setMyOrganizations(await asgardeo.getMyOrganizations());
1494
+ } finally {
1495
+ setIsLoadingSync(asgardeo.isLoading());
1496
+ }
1454
1497
  };
1455
1498
  const fetchBranding = useCallback7(async () => {
1456
1499
  if (!baseUrl) {
@@ -1498,6 +1541,7 @@ var AsgardeoProvider = ({
1498
1541
  ]);
1499
1542
  const signIn = async (...args) => {
1500
1543
  try {
1544
+ setIsLoadingSync(true);
1501
1545
  const response = await asgardeo.signIn(...args);
1502
1546
  if (await asgardeo.isSignedIn()) {
1503
1547
  await updateSession();
@@ -1505,6 +1549,27 @@ var AsgardeoProvider = ({
1505
1549
  return response;
1506
1550
  } catch (error) {
1507
1551
  throw new Error(`Error while signing in: ${error}`);
1552
+ } finally {
1553
+ setIsLoadingSync(asgardeo.isLoading());
1554
+ }
1555
+ };
1556
+ const signInSilently = async (options) => {
1557
+ try {
1558
+ setIsLoadingSync(true);
1559
+ const response = await asgardeo.signInSilently(options);
1560
+ if (await asgardeo.isSignedIn()) {
1561
+ await updateSession();
1562
+ }
1563
+ return response;
1564
+ } catch (error) {
1565
+ throw new AsgardeoRuntimeError3(
1566
+ `Error while signing in silently: ${error.message || error}`,
1567
+ "asgardeo-signInSilently-Error",
1568
+ "react",
1569
+ "An error occurred while trying to sign in silently."
1570
+ );
1571
+ } finally {
1572
+ setIsLoadingSync(asgardeo.isLoading());
1508
1573
  }
1509
1574
  };
1510
1575
  const signUp = async (payload) => {
@@ -1522,6 +1587,7 @@ var AsgardeoProvider = ({
1522
1587
  const signOut = async (options, afterSignOut) => asgardeo.signOut(options, afterSignOut);
1523
1588
  const switchOrganization = async (organization) => {
1524
1589
  try {
1590
+ setIsLoadingSync(true);
1525
1591
  await asgardeo.switchOrganization(organization);
1526
1592
  if (await asgardeo.isSignedIn()) {
1527
1593
  await updateSession();
@@ -1533,6 +1599,8 @@ var AsgardeoProvider = ({
1533
1599
  "react",
1534
1600
  "An error occurred while switching to the specified organization."
1535
1601
  );
1602
+ } finally {
1603
+ setIsLoadingSync(asgardeo.isLoading());
1536
1604
  }
1537
1605
  };
1538
1606
  const isDarkMode = useMemo6(() => {
@@ -1549,6 +1617,9 @@ var AsgardeoProvider = ({
1549
1617
  flattenedProfile: generateFlattenedUserProfile2(payload, prev?.schemas)
1550
1618
  }));
1551
1619
  };
1620
+ const fetch = async (url, options) => {
1621
+ return asgardeo.fetch(url, options);
1622
+ };
1552
1623
  return /* @__PURE__ */ jsx7(
1553
1624
  AsgardeoContext_default.Provider,
1554
1625
  {
@@ -1560,13 +1631,15 @@ var AsgardeoProvider = ({
1560
1631
  afterSignInUrl,
1561
1632
  baseUrl,
1562
1633
  isInitialized: isInitializedSync,
1563
- isLoading: asgardeo.isLoading(),
1634
+ isLoading: isLoadingSync,
1564
1635
  isSignedIn: isSignedInSync,
1565
1636
  organization: currentOrganization,
1566
1637
  signIn,
1638
+ signInSilently,
1567
1639
  signOut,
1568
1640
  signUp,
1569
- user
1641
+ user,
1642
+ fetch
1570
1643
  },
1571
1644
  children: /* @__PURE__ */ jsx7(I18nProvider_default, { preferences: preferences?.i18n, children: /* @__PURE__ */ jsx7(
1572
1645
  BrandingProvider_default,
@@ -11015,6 +11088,9 @@ var OrganizationSwitcher = ({
11015
11088
  ] });
11016
11089
  };
11017
11090
  var OrganizationSwitcher_default = OrganizationSwitcher;
11091
+
11092
+ // src/index.ts
11093
+ import { AsgardeoRuntimeError as AsgardeoRuntimeError7 } from "@asgardeo/browser";
11018
11094
  export {
11019
11095
  Alert_default as Alert,
11020
11096
  AlertDescription,
@@ -11022,6 +11098,7 @@ export {
11022
11098
  AsgardeoContext_default as AsgardeoContext,
11023
11099
  AsgardeoLoading_default as AsgardeoLoading,
11024
11100
  AsgardeoProvider_default as AsgardeoProvider,
11101
+ AsgardeoRuntimeError7 as AsgardeoRuntimeError,
11025
11102
  BaseCreateOrganization,
11026
11103
  BaseOrganization_default as BaseOrganization,
11027
11104
  BaseOrganizationList_default as BaseOrganizationList,