@asgardeo/react 0.5.6 → 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") {
@@ -1092,16 +1112,17 @@ var import_react7 = require("react");
1092
1112
  // src/contexts/Organization/OrganizationContext.ts
1093
1113
  var import_react6 = require("react");
1094
1114
  var OrganizationContext = (0, import_react6.createContext)({
1115
+ createOrganization: () => null,
1095
1116
  currentOrganization: null,
1096
1117
  error: null,
1097
- isLoading: false,
1098
- myOrganizations: null,
1099
- switchOrganization: () => Promise.resolve(),
1100
- revalidateMyOrganizations: () => Promise.resolve([]),
1101
1118
  getAllOrganizations: () => Promise.resolve({
1102
1119
  count: 0,
1103
1120
  organizations: []
1104
- })
1121
+ }),
1122
+ isLoading: false,
1123
+ myOrganizations: null,
1124
+ revalidateMyOrganizations: () => Promise.resolve([]),
1125
+ switchOrganization: () => Promise.resolve()
1105
1126
  });
1106
1127
  OrganizationContext.displayName = "OrganizationContext";
1107
1128
  var OrganizationContext_default = OrganizationContext;
@@ -1115,7 +1136,8 @@ var OrganizationProvider = ({
1115
1136
  myOrganizations,
1116
1137
  onOrganizationSwitch,
1117
1138
  revalidateMyOrganizations,
1118
- getAllOrganizations: getAllOrganizations2
1139
+ getAllOrganizations: getAllOrganizations2,
1140
+ createOrganization: createOrganization2
1119
1141
  }) => {
1120
1142
  const [isLoading, setIsLoading] = (0, import_react7.useState)(false);
1121
1143
  const [error, setError] = (0, import_react7.useState)(null);
@@ -1148,13 +1170,14 @@ var OrganizationProvider = ({
1148
1170
  );
1149
1171
  const contextValue = (0, import_react7.useMemo)(
1150
1172
  () => ({
1173
+ createOrganization: createOrganization2,
1151
1174
  currentOrganization,
1152
1175
  error,
1176
+ getAllOrganizations: getAllOrganizations2,
1153
1177
  isLoading,
1154
1178
  myOrganizations,
1155
- switchOrganization,
1156
1179
  revalidateMyOrganizations,
1157
- getAllOrganizations: getAllOrganizations2
1180
+ switchOrganization
1158
1181
  }),
1159
1182
  [
1160
1183
  currentOrganization,
@@ -1163,7 +1186,8 @@ var OrganizationProvider = ({
1163
1186
  myOrganizations,
1164
1187
  switchOrganization,
1165
1188
  revalidateMyOrganizations,
1166
- getAllOrganizations2
1189
+ getAllOrganizations2,
1190
+ createOrganization2
1167
1191
  ]
1168
1192
  );
1169
1193
  return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(OrganizationContext_default.Provider, { value: contextValue, children });
@@ -1455,6 +1479,7 @@ var AsgardeoProvider = ({
1455
1479
  const [currentOrganization, setCurrentOrganization] = (0, import_react15.useState)(null);
1456
1480
  const [isSignedInSync, setIsSignedInSync] = (0, import_react15.useState)(false);
1457
1481
  const [isInitializedSync, setIsInitializedSync] = (0, import_react15.useState)(false);
1482
+ const [isLoadingSync, setIsLoadingSync] = (0, import_react15.useState)(true);
1458
1483
  const [myOrganizations, setMyOrganizations] = (0, import_react15.useState)([]);
1459
1484
  const [userProfile, setUserProfile] = (0, import_react15.useState)(null);
1460
1485
  const [baseUrl, setBaseUrl] = (0, import_react15.useState)(_baseUrl);
@@ -1548,16 +1573,32 @@ var AsgardeoProvider = ({
1548
1573
  }
1549
1574
  })();
1550
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]);
1551
1587
  const updateSession = async () => {
1552
- let _baseUrl2 = baseUrl;
1553
- if ((await asgardeo.getDecodedIdToken())?.["user_org"]) {
1554
- _baseUrl2 = `${(await asgardeo.getConfiguration()).baseUrl}/o`;
1555
- setBaseUrl(_baseUrl2);
1556
- }
1557
- setUser(await asgardeo.getUser({ baseUrl: _baseUrl2 }));
1558
- setUserProfile(await asgardeo.getUserProfile({ baseUrl: _baseUrl2 }));
1559
- setCurrentOrganization(await asgardeo.getCurrentOrganization());
1560
- 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
+ }
1561
1602
  };
1562
1603
  const fetchBranding = (0, import_react15.useCallback)(async () => {
1563
1604
  if (!baseUrl) {
@@ -1605,6 +1646,7 @@ var AsgardeoProvider = ({
1605
1646
  ]);
1606
1647
  const signIn = async (...args) => {
1607
1648
  try {
1649
+ setIsLoadingSync(true);
1608
1650
  const response = await asgardeo.signIn(...args);
1609
1651
  if (await asgardeo.isSignedIn()) {
1610
1652
  await updateSession();
@@ -1612,6 +1654,27 @@ var AsgardeoProvider = ({
1612
1654
  return response;
1613
1655
  } catch (error) {
1614
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());
1615
1678
  }
1616
1679
  };
1617
1680
  const signUp = async (payload) => {
@@ -1629,6 +1692,7 @@ var AsgardeoProvider = ({
1629
1692
  const signOut = async (options, afterSignOut) => asgardeo.signOut(options, afterSignOut);
1630
1693
  const switchOrganization = async (organization) => {
1631
1694
  try {
1695
+ setIsLoadingSync(true);
1632
1696
  await asgardeo.switchOrganization(organization);
1633
1697
  if (await asgardeo.isSignedIn()) {
1634
1698
  await updateSession();
@@ -1640,6 +1704,8 @@ var AsgardeoProvider = ({
1640
1704
  "react",
1641
1705
  "An error occurred while switching to the specified organization."
1642
1706
  );
1707
+ } finally {
1708
+ setIsLoadingSync(asgardeo.isLoading());
1643
1709
  }
1644
1710
  };
1645
1711
  const isDarkMode = (0, import_react15.useMemo)(() => {
@@ -1667,10 +1733,11 @@ var AsgardeoProvider = ({
1667
1733
  afterSignInUrl,
1668
1734
  baseUrl,
1669
1735
  isInitialized: isInitializedSync,
1670
- isLoading: asgardeo.isLoading(),
1736
+ isLoading: isLoadingSync,
1671
1737
  isSignedIn: isSignedInSync,
1672
1738
  organization: currentOrganization,
1673
1739
  signIn,
1740
+ signInSilently,
1674
1741
  signOut,
1675
1742
  signUp,
1676
1743
  user
@@ -11039,4 +11106,7 @@ var OrganizationSwitcher = ({
11039
11106
  ] });
11040
11107
  };
11041
11108
  var OrganizationSwitcher_default = OrganizationSwitcher;
11109
+
11110
+ // src/index.ts
11111
+ var import_browser66 = require("@asgardeo/browser");
11042
11112
  //# sourceMappingURL=index.js.map