@asgardeo/react 0.5.7 → 0.5.8

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.
@@ -25,7 +25,19 @@ import { AsgardeoReactConfig } from './models/config';
25
25
  */
26
26
  declare class AsgardeoReactClient<T extends AsgardeoReactConfig = AsgardeoReactConfig> extends AsgardeoBrowserClient<T> {
27
27
  private asgardeo;
28
+ private _isLoading;
28
29
  constructor();
30
+ /**
31
+ * Set the loading state of the client
32
+ * @param loading - Boolean indicating if the client is in a loading state
33
+ */
34
+ private setLoading;
35
+ /**
36
+ * Wrap async operations with loading state management
37
+ * @param operation - The async operation to execute
38
+ * @returns Promise with the result of the operation
39
+ */
40
+ private withLoading;
29
41
  initialize(config: AsgardeoReactConfig): Promise<boolean>;
30
42
  updateUserProfile(payload: any, userId?: string): Promise<User>;
31
43
  getUser(options?: any): Promise<User>;
@@ -41,6 +53,7 @@ declare class AsgardeoReactClient<T extends AsgardeoReactConfig = AsgardeoReactC
41
53
  getConfiguration(): T;
42
54
  signIn(options?: SignInOptions, sessionId?: string, onSignInSuccess?: (afterSignInUrl: string) => void): Promise<User>;
43
55
  signIn(payload: EmbeddedSignInFlowHandleRequestPayload, request: EmbeddedFlowExecuteRequestConfig, sessionId?: string, onSignInSuccess?: (afterSignInUrl: string) => void): Promise<User>;
56
+ signInSilently(options?: SignInOptions): Promise<User | boolean>;
44
57
  signOut(options?: SignOutOptions, afterSignOut?: (afterSignOutUrl: string) => void): Promise<string>;
45
58
  signOut(options?: SignOutOptions, sessionId?: string, afterSignOut?: (afterSignOutUrl: string) => void): Promise<string>;
46
59
  signUp(options?: SignUpOptions): Promise<void>;
@@ -224,10 +224,10 @@ declare class AuthAPI {
224
224
  *
225
225
  * @example
226
226
  *```
227
- * client.trySignInSilently()
227
+ * client.signInSilently()
228
228
  *```
229
229
  */
230
- trySignInSilently(state: AuthStateInterface, dispatch: (state: AuthStateInterface) => void, additionalParams?: Record<string, string | boolean>, tokenRequestConfig?: {
230
+ signInSilently(additionalParams?: Record<string, string | boolean>, tokenRequestConfig?: {
231
231
  params: Record<string, unknown>;
232
232
  }): Promise<User | boolean | undefined>;
233
233
  }
@@ -78,7 +78,7 @@ export interface AuthContextInterface {
78
78
  enableHttpHandler(): Promise<boolean>;
79
79
  disableHttpHandler(): Promise<boolean>;
80
80
  reInitialize(config: Partial<AuthClientConfig<Config>>): Promise<void>;
81
- trySignInSilently: (additionalParams?: Record<string, string | boolean>, tokenRequestConfig?: {
81
+ signInSilently: (additionalParams?: Record<string, string | boolean>, tokenRequestConfig?: {
82
82
  params: Record<string, unknown>;
83
83
  }) => Promise<boolean | User>;
84
84
  on(hook: Hooks.CustomGrant, callback: (response?: any) => void, id: string): void;
package/dist/cjs/index.js CHANGED
@@ -37,6 +37,7 @@ __export(index_exports, {
37
37
  AsgardeoContext: () => AsgardeoContext_default,
38
38
  AsgardeoLoading: () => AsgardeoLoading_default,
39
39
  AsgardeoProvider: () => AsgardeoProvider_default,
40
+ AsgardeoRuntimeError: () => import_browser66.AsgardeoRuntimeError,
40
41
  BaseCreateOrganization: () => BaseCreateOrganization,
41
42
  BaseOrganization: () => BaseOrganization_default,
42
43
  BaseOrganizationList: () => BaseOrganizationList_default,
@@ -167,6 +168,7 @@ var AsgardeoContext = (0, import_react.createContext)({
167
168
  isSignedIn: false,
168
169
  organization: null,
169
170
  signIn: null,
171
+ signInSilently: null,
170
172
  signOut: null,
171
173
  signUp: null,
172
174
  user: null
@@ -486,29 +488,14 @@ var _AuthAPI = class _AuthAPI {
486
488
  *
487
489
  * @example
488
490
  *```
489
- * client.trySignInSilently()
491
+ * client.signInSilently()
490
492
  *```
491
493
  */
492
- async trySignInSilently(state, dispatch, additionalParams, tokenRequestConfig) {
493
- return this._client.trySignInSilently(additionalParams, tokenRequestConfig).then(async (response) => {
494
+ async signInSilently(additionalParams, tokenRequestConfig) {
495
+ return this._client.signInSilently(additionalParams, tokenRequestConfig).then(async (response) => {
494
496
  if (!response) {
495
- this.updateState({ ...this.getState(), isLoading: false });
496
- dispatch({ ...state, isLoading: false });
497
497
  return false;
498
498
  }
499
- if (await this._client.isSignedIn()) {
500
- const basicUserInfo = response;
501
- const stateToUpdate = {
502
- displayName: basicUserInfo.displayName,
503
- email: basicUserInfo.email,
504
- isSignedIn: true,
505
- isLoading: false,
506
- isSigningOut: false,
507
- username: basicUserInfo.username
508
- };
509
- this.updateState(stateToUpdate);
510
- dispatch({ ...state, ...stateToUpdate });
511
- }
512
499
  return response;
513
500
  }).catch((error) => Promise.reject(error));
514
501
  }
@@ -632,14 +619,38 @@ var AsgardeoReactClient = class extends import_browser6.AsgardeoBrowserClient {
632
619
  constructor() {
633
620
  super();
634
621
  __publicField(this, "asgardeo");
622
+ __publicField(this, "_isLoading", false);
635
623
  this.asgardeo = new api_default();
636
624
  }
625
+ /**
626
+ * Set the loading state of the client
627
+ * @param loading - Boolean indicating if the client is in a loading state
628
+ */
629
+ setLoading(loading) {
630
+ this._isLoading = loading;
631
+ }
632
+ /**
633
+ * Wrap async operations with loading state management
634
+ * @param operation - The async operation to execute
635
+ * @returns Promise with the result of the operation
636
+ */
637
+ async withLoading(operation) {
638
+ this.setLoading(true);
639
+ try {
640
+ const result = await operation();
641
+ return result;
642
+ } finally {
643
+ this.setLoading(false);
644
+ }
645
+ }
637
646
  initialize(config) {
638
647
  let resolvedOrganizationHandle = config?.organizationHandle;
639
648
  if (!resolvedOrganizationHandle) {
640
649
  resolvedOrganizationHandle = (0, import_browser6.deriveOrganizationHandleFromBaseUrl)(config?.baseUrl);
641
650
  }
642
- return this.asgardeo.init({ ...config, organizationHandle: resolvedOrganizationHandle });
651
+ return this.withLoading(async () => {
652
+ return this.asgardeo.init({ ...config, organizationHandle: resolvedOrganizationHandle });
653
+ });
643
654
  }
644
655
  async updateUserProfile(payload, userId) {
645
656
  throw new Error("Not implemented");
@@ -728,47 +739,49 @@ var AsgardeoReactClient = class extends import_browser6.AsgardeoBrowserClient {
728
739
  };
729
740
  }
730
741
  async switchOrganization(organization, sessionId) {
731
- try {
732
- const configData = await this.asgardeo.getConfigData();
733
- const scopes = configData?.scopes;
734
- if (!organization.id) {
742
+ return this.withLoading(async () => {
743
+ try {
744
+ const configData = await this.asgardeo.getConfigData();
745
+ const scopes = configData?.scopes;
746
+ if (!organization.id) {
747
+ throw new import_browser6.AsgardeoRuntimeError(
748
+ "Organization ID is required for switching organizations",
749
+ "react-AsgardeoReactClient-SwitchOrganizationError-001",
750
+ "react",
751
+ "The organization object must contain a valid ID to perform the organization switch."
752
+ );
753
+ }
754
+ const exchangeConfig = {
755
+ attachToken: false,
756
+ data: {
757
+ client_id: "{{clientId}}",
758
+ grant_type: "organization_switch",
759
+ scope: "{{scopes}}",
760
+ switching_organization: organization.id,
761
+ token: "{{accessToken}}"
762
+ },
763
+ id: "organization-switch",
764
+ returnsSession: true,
765
+ signInRequired: true
766
+ };
767
+ return await this.asgardeo.exchangeToken(
768
+ exchangeConfig,
769
+ (user) => {
770
+ },
771
+ () => null
772
+ );
773
+ } catch (error) {
735
774
  throw new import_browser6.AsgardeoRuntimeError(
736
- "Organization ID is required for switching organizations",
737
- "react-AsgardeoReactClient-SwitchOrganizationError-001",
775
+ `Failed to switch organization: ${error.message || error}`,
776
+ "react-AsgardeoReactClient-SwitchOrganizationError-003",
738
777
  "react",
739
- "The organization object must contain a valid ID to perform the organization switch."
778
+ "An error occurred while switching to the specified organization. Please try again."
740
779
  );
741
780
  }
742
- const exchangeConfig = {
743
- attachToken: false,
744
- data: {
745
- client_id: "{{clientId}}",
746
- grant_type: "organization_switch",
747
- scope: "{{scopes}}",
748
- switching_organization: organization.id,
749
- token: "{{accessToken}}"
750
- },
751
- id: "organization-switch",
752
- returnsSession: true,
753
- signInRequired: true
754
- };
755
- return await this.asgardeo.exchangeToken(
756
- exchangeConfig,
757
- (user) => {
758
- },
759
- () => null
760
- );
761
- } catch (error) {
762
- throw new import_browser6.AsgardeoRuntimeError(
763
- `Failed to switch organization: ${error.message || error}`,
764
- "react-AsgardeoReactClient-SwitchOrganizationError-003",
765
- "react",
766
- "An error occurred while switching to the specified organization. Please try again."
767
- );
768
- }
781
+ });
769
782
  }
770
783
  isLoading() {
771
- return this.asgardeo.isLoading();
784
+ return this._isLoading || this.asgardeo.isLoading();
772
785
  }
773
786
  async isInitialized() {
774
787
  return this.asgardeo.isInitialized();
@@ -780,15 +793,22 @@ var AsgardeoReactClient = class extends import_browser6.AsgardeoBrowserClient {
780
793
  return this.asgardeo.getConfigData();
781
794
  }
782
795
  async signIn(...args) {
783
- const arg1 = args[0];
784
- const arg2 = args[1];
785
- if (typeof arg1 === "object" && "flowId" in arg1 && typeof arg2 === "object" && "url" in arg2) {
786
- return (0, import_browser6.executeEmbeddedSignInFlow)({
787
- payload: arg1,
788
- url: arg2.url
789
- });
790
- }
791
- return await this.asgardeo.signIn(arg1);
796
+ return this.withLoading(async () => {
797
+ const arg1 = args[0];
798
+ const arg2 = args[1];
799
+ if (typeof arg1 === "object" && "flowId" in arg1 && typeof arg2 === "object" && "url" in arg2) {
800
+ return (0, import_browser6.executeEmbeddedSignInFlow)({
801
+ payload: arg1,
802
+ url: arg2.url
803
+ });
804
+ }
805
+ return await this.asgardeo.signIn(arg1);
806
+ });
807
+ }
808
+ async signInSilently(options) {
809
+ return this.withLoading(async () => {
810
+ return this.asgardeo.signInSilently(options);
811
+ });
792
812
  }
793
813
  async signOut(...args) {
794
814
  if (args[1] && typeof args[1] !== "function") {
@@ -1459,6 +1479,7 @@ var AsgardeoProvider = ({
1459
1479
  const [currentOrganization, setCurrentOrganization] = (0, import_react15.useState)(null);
1460
1480
  const [isSignedInSync, setIsSignedInSync] = (0, import_react15.useState)(false);
1461
1481
  const [isInitializedSync, setIsInitializedSync] = (0, import_react15.useState)(false);
1482
+ const [isLoadingSync, setIsLoadingSync] = (0, import_react15.useState)(true);
1462
1483
  const [myOrganizations, setMyOrganizations] = (0, import_react15.useState)([]);
1463
1484
  const [userProfile, setUserProfile] = (0, import_react15.useState)(null);
1464
1485
  const [baseUrl, setBaseUrl] = (0, import_react15.useState)(_baseUrl);
@@ -1552,16 +1573,32 @@ var AsgardeoProvider = ({
1552
1573
  }
1553
1574
  })();
1554
1575
  }, [asgardeo]);
1576
+ (0, import_react15.useEffect)(() => {
1577
+ const checkLoadingState = () => {
1578
+ const loadingState = asgardeo.isLoading();
1579
+ setIsLoadingSync(loadingState);
1580
+ };
1581
+ checkLoadingState();
1582
+ const interval = setInterval(checkLoadingState, 100);
1583
+ return () => {
1584
+ clearInterval(interval);
1585
+ };
1586
+ }, [asgardeo]);
1555
1587
  const updateSession = async () => {
1556
- let _baseUrl2 = baseUrl;
1557
- if ((await asgardeo.getDecodedIdToken())?.["user_org"]) {
1558
- _baseUrl2 = `${(await asgardeo.getConfiguration()).baseUrl}/o`;
1559
- setBaseUrl(_baseUrl2);
1560
- }
1561
- setUser(await asgardeo.getUser({ baseUrl: _baseUrl2 }));
1562
- setUserProfile(await asgardeo.getUserProfile({ baseUrl: _baseUrl2 }));
1563
- setCurrentOrganization(await asgardeo.getCurrentOrganization());
1564
- setMyOrganizations(await asgardeo.getMyOrganizations());
1588
+ try {
1589
+ setIsLoadingSync(true);
1590
+ let _baseUrl2 = baseUrl;
1591
+ if ((await asgardeo.getDecodedIdToken())?.["user_org"]) {
1592
+ _baseUrl2 = `${(await asgardeo.getConfiguration()).baseUrl}/o`;
1593
+ setBaseUrl(_baseUrl2);
1594
+ }
1595
+ setUser(await asgardeo.getUser({ baseUrl: _baseUrl2 }));
1596
+ setUserProfile(await asgardeo.getUserProfile({ baseUrl: _baseUrl2 }));
1597
+ setCurrentOrganization(await asgardeo.getCurrentOrganization());
1598
+ setMyOrganizations(await asgardeo.getMyOrganizations());
1599
+ } finally {
1600
+ setIsLoadingSync(asgardeo.isLoading());
1601
+ }
1565
1602
  };
1566
1603
  const fetchBranding = (0, import_react15.useCallback)(async () => {
1567
1604
  if (!baseUrl) {
@@ -1609,6 +1646,7 @@ var AsgardeoProvider = ({
1609
1646
  ]);
1610
1647
  const signIn = async (...args) => {
1611
1648
  try {
1649
+ setIsLoadingSync(true);
1612
1650
  const response = await asgardeo.signIn(...args);
1613
1651
  if (await asgardeo.isSignedIn()) {
1614
1652
  await updateSession();
@@ -1616,6 +1654,27 @@ var AsgardeoProvider = ({
1616
1654
  return response;
1617
1655
  } catch (error) {
1618
1656
  throw new Error(`Error while signing in: ${error}`);
1657
+ } finally {
1658
+ setIsLoadingSync(asgardeo.isLoading());
1659
+ }
1660
+ };
1661
+ const signInSilently = async (options) => {
1662
+ try {
1663
+ setIsLoadingSync(true);
1664
+ const response = await asgardeo.signInSilently(options);
1665
+ if (await asgardeo.isSignedIn()) {
1666
+ await updateSession();
1667
+ }
1668
+ return response;
1669
+ } catch (error) {
1670
+ throw new import_browser13.AsgardeoRuntimeError(
1671
+ `Error while signing in silently: ${error.message || error}`,
1672
+ "asgardeo-signInSilently-Error",
1673
+ "react",
1674
+ "An error occurred while trying to sign in silently."
1675
+ );
1676
+ } finally {
1677
+ setIsLoadingSync(asgardeo.isLoading());
1619
1678
  }
1620
1679
  };
1621
1680
  const signUp = async (payload) => {
@@ -1633,6 +1692,7 @@ var AsgardeoProvider = ({
1633
1692
  const signOut = async (options, afterSignOut) => asgardeo.signOut(options, afterSignOut);
1634
1693
  const switchOrganization = async (organization) => {
1635
1694
  try {
1695
+ setIsLoadingSync(true);
1636
1696
  await asgardeo.switchOrganization(organization);
1637
1697
  if (await asgardeo.isSignedIn()) {
1638
1698
  await updateSession();
@@ -1644,6 +1704,8 @@ var AsgardeoProvider = ({
1644
1704
  "react",
1645
1705
  "An error occurred while switching to the specified organization."
1646
1706
  );
1707
+ } finally {
1708
+ setIsLoadingSync(asgardeo.isLoading());
1647
1709
  }
1648
1710
  };
1649
1711
  const isDarkMode = (0, import_react15.useMemo)(() => {
@@ -1671,10 +1733,11 @@ var AsgardeoProvider = ({
1671
1733
  afterSignInUrl,
1672
1734
  baseUrl,
1673
1735
  isInitialized: isInitializedSync,
1674
- isLoading: asgardeo.isLoading(),
1736
+ isLoading: isLoadingSync,
1675
1737
  isSignedIn: isSignedInSync,
1676
1738
  organization: currentOrganization,
1677
1739
  signIn,
1740
+ signInSilently,
1678
1741
  signOut,
1679
1742
  signUp,
1680
1743
  user
@@ -11043,4 +11106,7 @@ var OrganizationSwitcher = ({
11043
11106
  ] });
11044
11107
  };
11045
11108
  var OrganizationSwitcher_default = OrganizationSwitcher;
11109
+
11110
+ // src/index.ts
11111
+ var import_browser66 = require("@asgardeo/browser");
11046
11112
  //# sourceMappingURL=index.js.map