@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.
@@ -17,6 +17,7 @@
17
17
  */
18
18
  import { Context } from 'react';
19
19
  import { 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.
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,6 +24,7 @@ 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
30
  user: null
@@ -356,29 +357,14 @@ var _AuthAPI = class _AuthAPI {
356
357
  *
357
358
  * @example
358
359
  *```
359
- * client.trySignInSilently()
360
+ * client.signInSilently()
360
361
  *```
361
362
  */
362
- async trySignInSilently(state, dispatch, additionalParams, tokenRequestConfig) {
363
- return this._client.trySignInSilently(additionalParams, tokenRequestConfig).then(async (response) => {
363
+ async signInSilently(additionalParams, tokenRequestConfig) {
364
+ return this._client.signInSilently(additionalParams, tokenRequestConfig).then(async (response) => {
364
365
  if (!response) {
365
- this.updateState({ ...this.getState(), isLoading: false });
366
- dispatch({ ...state, isLoading: false });
367
366
  return false;
368
367
  }
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
368
  return response;
383
369
  }).catch((error) => Promise.reject(error));
384
370
  }
@@ -514,14 +500,38 @@ var AsgardeoReactClient = class extends AsgardeoBrowserClient {
514
500
  constructor() {
515
501
  super();
516
502
  __publicField(this, "asgardeo");
503
+ __publicField(this, "_isLoading", false);
517
504
  this.asgardeo = new api_default();
518
505
  }
506
+ /**
507
+ * Set the loading state of the client
508
+ * @param loading - Boolean indicating if the client is in a loading state
509
+ */
510
+ setLoading(loading) {
511
+ this._isLoading = loading;
512
+ }
513
+ /**
514
+ * Wrap async operations with loading state management
515
+ * @param operation - The async operation to execute
516
+ * @returns Promise with the result of the operation
517
+ */
518
+ async withLoading(operation) {
519
+ this.setLoading(true);
520
+ try {
521
+ const result = await operation();
522
+ return result;
523
+ } finally {
524
+ this.setLoading(false);
525
+ }
526
+ }
519
527
  initialize(config) {
520
528
  let resolvedOrganizationHandle = config?.organizationHandle;
521
529
  if (!resolvedOrganizationHandle) {
522
530
  resolvedOrganizationHandle = deriveOrganizationHandleFromBaseUrl(config?.baseUrl);
523
531
  }
524
- return this.asgardeo.init({ ...config, organizationHandle: resolvedOrganizationHandle });
532
+ return this.withLoading(async () => {
533
+ return this.asgardeo.init({ ...config, organizationHandle: resolvedOrganizationHandle });
534
+ });
525
535
  }
526
536
  async updateUserProfile(payload, userId) {
527
537
  throw new Error("Not implemented");
@@ -610,47 +620,49 @@ var AsgardeoReactClient = class extends AsgardeoBrowserClient {
610
620
  };
611
621
  }
612
622
  async switchOrganization(organization, sessionId) {
613
- try {
614
- const configData = await this.asgardeo.getConfigData();
615
- const scopes = configData?.scopes;
616
- if (!organization.id) {
623
+ return this.withLoading(async () => {
624
+ try {
625
+ const configData = await this.asgardeo.getConfigData();
626
+ const scopes = configData?.scopes;
627
+ if (!organization.id) {
628
+ throw new AsgardeoRuntimeError(
629
+ "Organization ID is required for switching organizations",
630
+ "react-AsgardeoReactClient-SwitchOrganizationError-001",
631
+ "react",
632
+ "The organization object must contain a valid ID to perform the organization switch."
633
+ );
634
+ }
635
+ const exchangeConfig = {
636
+ attachToken: false,
637
+ data: {
638
+ client_id: "{{clientId}}",
639
+ grant_type: "organization_switch",
640
+ scope: "{{scopes}}",
641
+ switching_organization: organization.id,
642
+ token: "{{accessToken}}"
643
+ },
644
+ id: "organization-switch",
645
+ returnsSession: true,
646
+ signInRequired: true
647
+ };
648
+ return await this.asgardeo.exchangeToken(
649
+ exchangeConfig,
650
+ (user) => {
651
+ },
652
+ () => null
653
+ );
654
+ } catch (error) {
617
655
  throw new AsgardeoRuntimeError(
618
- "Organization ID is required for switching organizations",
619
- "react-AsgardeoReactClient-SwitchOrganizationError-001",
656
+ `Failed to switch organization: ${error.message || error}`,
657
+ "react-AsgardeoReactClient-SwitchOrganizationError-003",
620
658
  "react",
621
- "The organization object must contain a valid ID to perform the organization switch."
659
+ "An error occurred while switching to the specified organization. Please try again."
622
660
  );
623
661
  }
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
- }
662
+ });
651
663
  }
652
664
  isLoading() {
653
- return this.asgardeo.isLoading();
665
+ return this._isLoading || this.asgardeo.isLoading();
654
666
  }
655
667
  async isInitialized() {
656
668
  return this.asgardeo.isInitialized();
@@ -662,15 +674,22 @@ var AsgardeoReactClient = class extends AsgardeoBrowserClient {
662
674
  return this.asgardeo.getConfigData();
663
675
  }
664
676
  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);
677
+ return this.withLoading(async () => {
678
+ const arg1 = args[0];
679
+ const arg2 = args[1];
680
+ if (typeof arg1 === "object" && "flowId" in arg1 && typeof arg2 === "object" && "url" in arg2) {
681
+ return executeEmbeddedSignInFlow({
682
+ payload: arg1,
683
+ url: arg2.url
684
+ });
685
+ }
686
+ return await this.asgardeo.signIn(arg1);
687
+ });
688
+ }
689
+ async signInSilently(options) {
690
+ return this.withLoading(async () => {
691
+ return this.asgardeo.signInSilently(options);
692
+ });
674
693
  }
675
694
  async signOut(...args) {
676
695
  if (args[1] && typeof args[1] !== "function") {
@@ -1348,6 +1367,7 @@ var AsgardeoProvider = ({
1348
1367
  const [currentOrganization, setCurrentOrganization] = useState7(null);
1349
1368
  const [isSignedInSync, setIsSignedInSync] = useState7(false);
1350
1369
  const [isInitializedSync, setIsInitializedSync] = useState7(false);
1370
+ const [isLoadingSync, setIsLoadingSync] = useState7(true);
1351
1371
  const [myOrganizations, setMyOrganizations] = useState7([]);
1352
1372
  const [userProfile, setUserProfile] = useState7(null);
1353
1373
  const [baseUrl, setBaseUrl] = useState7(_baseUrl);
@@ -1441,16 +1461,32 @@ var AsgardeoProvider = ({
1441
1461
  }
1442
1462
  })();
1443
1463
  }, [asgardeo]);
1464
+ useEffect5(() => {
1465
+ const checkLoadingState = () => {
1466
+ const loadingState = asgardeo.isLoading();
1467
+ setIsLoadingSync(loadingState);
1468
+ };
1469
+ checkLoadingState();
1470
+ const interval = setInterval(checkLoadingState, 100);
1471
+ return () => {
1472
+ clearInterval(interval);
1473
+ };
1474
+ }, [asgardeo]);
1444
1475
  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());
1476
+ try {
1477
+ setIsLoadingSync(true);
1478
+ let _baseUrl2 = baseUrl;
1479
+ if ((await asgardeo.getDecodedIdToken())?.["user_org"]) {
1480
+ _baseUrl2 = `${(await asgardeo.getConfiguration()).baseUrl}/o`;
1481
+ setBaseUrl(_baseUrl2);
1482
+ }
1483
+ setUser(await asgardeo.getUser({ baseUrl: _baseUrl2 }));
1484
+ setUserProfile(await asgardeo.getUserProfile({ baseUrl: _baseUrl2 }));
1485
+ setCurrentOrganization(await asgardeo.getCurrentOrganization());
1486
+ setMyOrganizations(await asgardeo.getMyOrganizations());
1487
+ } finally {
1488
+ setIsLoadingSync(asgardeo.isLoading());
1489
+ }
1454
1490
  };
1455
1491
  const fetchBranding = useCallback7(async () => {
1456
1492
  if (!baseUrl) {
@@ -1498,6 +1534,7 @@ var AsgardeoProvider = ({
1498
1534
  ]);
1499
1535
  const signIn = async (...args) => {
1500
1536
  try {
1537
+ setIsLoadingSync(true);
1501
1538
  const response = await asgardeo.signIn(...args);
1502
1539
  if (await asgardeo.isSignedIn()) {
1503
1540
  await updateSession();
@@ -1505,6 +1542,27 @@ var AsgardeoProvider = ({
1505
1542
  return response;
1506
1543
  } catch (error) {
1507
1544
  throw new Error(`Error while signing in: ${error}`);
1545
+ } finally {
1546
+ setIsLoadingSync(asgardeo.isLoading());
1547
+ }
1548
+ };
1549
+ const signInSilently = async (options) => {
1550
+ try {
1551
+ setIsLoadingSync(true);
1552
+ const response = await asgardeo.signInSilently(options);
1553
+ if (await asgardeo.isSignedIn()) {
1554
+ await updateSession();
1555
+ }
1556
+ return response;
1557
+ } catch (error) {
1558
+ throw new AsgardeoRuntimeError3(
1559
+ `Error while signing in silently: ${error.message || error}`,
1560
+ "asgardeo-signInSilently-Error",
1561
+ "react",
1562
+ "An error occurred while trying to sign in silently."
1563
+ );
1564
+ } finally {
1565
+ setIsLoadingSync(asgardeo.isLoading());
1508
1566
  }
1509
1567
  };
1510
1568
  const signUp = async (payload) => {
@@ -1522,6 +1580,7 @@ var AsgardeoProvider = ({
1522
1580
  const signOut = async (options, afterSignOut) => asgardeo.signOut(options, afterSignOut);
1523
1581
  const switchOrganization = async (organization) => {
1524
1582
  try {
1583
+ setIsLoadingSync(true);
1525
1584
  await asgardeo.switchOrganization(organization);
1526
1585
  if (await asgardeo.isSignedIn()) {
1527
1586
  await updateSession();
@@ -1533,6 +1592,8 @@ var AsgardeoProvider = ({
1533
1592
  "react",
1534
1593
  "An error occurred while switching to the specified organization."
1535
1594
  );
1595
+ } finally {
1596
+ setIsLoadingSync(asgardeo.isLoading());
1536
1597
  }
1537
1598
  };
1538
1599
  const isDarkMode = useMemo6(() => {
@@ -1560,10 +1621,11 @@ var AsgardeoProvider = ({
1560
1621
  afterSignInUrl,
1561
1622
  baseUrl,
1562
1623
  isInitialized: isInitializedSync,
1563
- isLoading: asgardeo.isLoading(),
1624
+ isLoading: isLoadingSync,
1564
1625
  isSignedIn: isSignedInSync,
1565
1626
  organization: currentOrganization,
1566
1627
  signIn,
1628
+ signInSilently,
1567
1629
  signOut,
1568
1630
  signUp,
1569
1631
  user
@@ -11015,6 +11077,9 @@ var OrganizationSwitcher = ({
11015
11077
  ] });
11016
11078
  };
11017
11079
  var OrganizationSwitcher_default = OrganizationSwitcher;
11080
+
11081
+ // src/index.ts
11082
+ import { AsgardeoRuntimeError as AsgardeoRuntimeError7 } from "@asgardeo/browser";
11018
11083
  export {
11019
11084
  Alert_default as Alert,
11020
11085
  AlertDescription,
@@ -11022,6 +11087,7 @@ export {
11022
11087
  AsgardeoContext_default as AsgardeoContext,
11023
11088
  AsgardeoLoading_default as AsgardeoLoading,
11024
11089
  AsgardeoProvider_default as AsgardeoProvider,
11090
+ AsgardeoRuntimeError7 as AsgardeoRuntimeError,
11025
11091
  BaseCreateOrganization,
11026
11092
  BaseOrganization_default as BaseOrganization,
11027
11093
  BaseOrganizationList_default as BaseOrganizationList,