@asgardeo/react 0.6.9 → 0.6.11

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.
package/dist/index.js CHANGED
@@ -1536,14 +1536,24 @@ var AsgardeoProvider = ({
1536
1536
  const currentUrl = new URL(window.location.href);
1537
1537
  const hasAuthParamsResult = hasAuthParams(currentUrl, afterSignInUrl);
1538
1538
  const isV2Platform = config.platform === Platform2.AsgardeoV2;
1539
- if (hasAuthParamsResult && !isV2Platform) {
1539
+ if (hasAuthParamsResult) {
1540
1540
  try {
1541
- await signIn(
1542
- { callOnlyOnRedirect: true }
1543
- // authParams?.authorizationCode,
1544
- // authParams?.sessionState,
1545
- // authParams?.state,
1546
- );
1541
+ if (isV2Platform) {
1542
+ const urlParams = currentUrl.searchParams;
1543
+ const code = urlParams.get("code");
1544
+ const flowIdFromUrl = urlParams.get("flowId");
1545
+ const storedFlowId = sessionStorage.getItem("asgardeo_flow_id");
1546
+ if (code && !flowIdFromUrl && !storedFlowId) {
1547
+ await signIn();
1548
+ }
1549
+ } else {
1550
+ await signIn(
1551
+ { callOnlyOnRedirect: true }
1552
+ // authParams?.authorizationCode,
1553
+ // authParams?.sessionState,
1554
+ // authParams?.state,
1555
+ );
1556
+ }
1547
1557
  } catch (error) {
1548
1558
  if (error && Object.prototype.hasOwnProperty.call(error, "code")) {
1549
1559
  }
@@ -7569,18 +7579,87 @@ var SignIn = ({ className, size = "medium", onSuccess, onError, variant, childre
7569
7579
  const [isSubmitting, setIsSubmitting] = useState16(false);
7570
7580
  const initializationAttemptedRef = useRef4(false);
7571
7581
  const oauthCodeProcessedRef = useRef4(false);
7582
+ const setFlowId = (flowId) => {
7583
+ setCurrentFlowId(flowId);
7584
+ if (flowId) {
7585
+ sessionStorage.setItem("asgardeo_flow_id", flowId);
7586
+ } else {
7587
+ sessionStorage.removeItem("asgardeo_flow_id");
7588
+ }
7589
+ };
7590
+ const clearFlowState = () => {
7591
+ setFlowId(null);
7592
+ setIsFlowInitialized(false);
7593
+ sessionStorage.removeItem("asgardeo_session_data_key");
7594
+ oauthCodeProcessedRef.current = false;
7595
+ };
7596
+ const getUrlParams = () => {
7597
+ const urlParams = new URL(window?.location?.href ?? "").searchParams;
7598
+ return {
7599
+ code: urlParams.get("code"),
7600
+ error: urlParams.get("error"),
7601
+ errorDescription: urlParams.get("error_description"),
7602
+ state: urlParams.get("state"),
7603
+ nonce: urlParams.get("nonce"),
7604
+ flowId: urlParams.get("flowId"),
7605
+ applicationId: urlParams.get("applicationId"),
7606
+ sessionDataKey: urlParams.get("sessionDataKey")
7607
+ };
7608
+ };
7609
+ const handleSessionDataKey = (sessionDataKey) => {
7610
+ if (sessionDataKey) {
7611
+ sessionStorage.setItem("asgardeo_session_data_key", sessionDataKey);
7612
+ }
7613
+ };
7614
+ const resolveFlowId = (currentFlowId2, state, flowIdFromUrl, storedFlowId) => {
7615
+ return currentFlowId2 || state || flowIdFromUrl || storedFlowId || null;
7616
+ };
7617
+ const cleanupOAuthUrlParams = (includeNonce = false) => {
7618
+ if (!window?.location?.href) return;
7619
+ const url = new URL(window.location.href);
7620
+ url.searchParams.delete("error");
7621
+ url.searchParams.delete("error_description");
7622
+ url.searchParams.delete("code");
7623
+ url.searchParams.delete("state");
7624
+ if (includeNonce) {
7625
+ url.searchParams.delete("nonce");
7626
+ }
7627
+ window?.history?.replaceState({}, "", url.toString());
7628
+ };
7629
+ const cleanupFlowUrlParams = () => {
7630
+ if (!window?.location?.href) return;
7631
+ const url = new URL(window.location.href);
7632
+ url.searchParams.delete("flowId");
7633
+ url.searchParams.delete("sessionDataKey");
7634
+ url.searchParams.delete("applicationId");
7635
+ window?.history?.replaceState({}, "", url.toString());
7636
+ };
7637
+ const handleOAuthError = (error, errorDescription) => {
7638
+ console.warn("[SignIn] OAuth error detected:", error);
7639
+ clearFlowState();
7640
+ const errorMessage = errorDescription || `OAuth error: ${error}`;
7641
+ const err = new AsgardeoRuntimeError8(
7642
+ errorMessage,
7643
+ "SIGN_IN_ERROR",
7644
+ "react"
7645
+ );
7646
+ setError(err);
7647
+ cleanupOAuthUrlParams(true);
7648
+ };
7649
+ const setError = (error) => {
7650
+ setFlowError(error);
7651
+ setIsFlowInitialized(true);
7652
+ onError?.(error);
7653
+ };
7572
7654
  const handleRedirection = (response) => {
7573
7655
  if (response.type === EmbeddedSignInFlowTypeV2.Redirection) {
7574
7656
  const redirectURL = response.data?.redirectURL || response?.redirectURL;
7575
- if (redirectURL) {
7657
+ if (redirectURL && window?.location) {
7576
7658
  if (response.flowId) {
7577
- sessionStorage.setItem("asgardeo_flow_id", response.flowId);
7578
- }
7579
- const urlParams = new URL(window.location.href).searchParams;
7580
- const sessionDataKeyFromUrl = urlParams.get("sessionDataKey");
7581
- if (sessionDataKeyFromUrl) {
7582
- sessionStorage.setItem("asgardeo_session_data_key", sessionDataKeyFromUrl);
7659
+ setFlowId(response.flowId);
7583
7660
  }
7661
+ const urlParams = getUrlParams();
7662
+ handleSessionDataKey(urlParams.sessionDataKey);
7584
7663
  window.location.href = redirectURL;
7585
7664
  return true;
7586
7665
  }
@@ -7589,58 +7668,41 @@ var SignIn = ({ className, size = "medium", onSuccess, onError, variant, childre
7589
7668
  };
7590
7669
  useEffect13(() => {
7591
7670
  const storedFlowId = sessionStorage.getItem("asgardeo_flow_id");
7592
- const urlParams = new URL(window.location.href).searchParams;
7593
- const code = urlParams.get("code");
7594
- const state = urlParams.get("state");
7595
- const sessionDataKeyFromUrl = urlParams.get("sessionDataKey");
7596
- if (sessionDataKeyFromUrl) {
7597
- sessionStorage.setItem("asgardeo_session_data_key", sessionDataKeyFromUrl);
7598
- }
7599
- if (code) {
7600
- const flowIdFromUrl = urlParams.get("flowId");
7601
- const flowIdFromState = state || flowIdFromUrl || storedFlowId;
7602
- if (flowIdFromState) {
7603
- setCurrentFlowId(flowIdFromState);
7604
- setIsFlowInitialized(true);
7605
- sessionStorage.setItem("asgardeo_flow_id", flowIdFromState);
7606
- initializationAttemptedRef.current = true;
7607
- }
7671
+ const urlParams = getUrlParams();
7672
+ if (urlParams.error) {
7673
+ handleOAuthError(urlParams.error, urlParams.errorDescription);
7674
+ return;
7675
+ }
7676
+ handleSessionDataKey(urlParams.sessionDataKey);
7677
+ if (urlParams.code || urlParams.state) {
7608
7678
  return;
7609
7679
  }
7610
- if (isInitialized && !isLoading && !isFlowInitialized && !initializationAttemptedRef.current && !currentFlowId) {
7680
+ const currentUrlParams = getUrlParams();
7681
+ if (isInitialized && !isLoading && !isFlowInitialized && !initializationAttemptedRef.current && !currentFlowId && !currentUrlParams.code && !currentUrlParams.state && !isSubmitting && !oauthCodeProcessedRef.current) {
7611
7682
  initializationAttemptedRef.current = true;
7612
7683
  initializeFlow();
7613
7684
  }
7614
7685
  }, [isInitialized, isLoading, isFlowInitialized, currentFlowId]);
7615
7686
  const initializeFlow = async () => {
7616
- const urlParams = new URL(window.location.href).searchParams;
7617
- const code = urlParams.get("code");
7618
- if (code) {
7619
- return;
7620
- }
7621
- const flowIdFromUrl = urlParams.get("flowId");
7622
- const applicationIdFromUrl = urlParams.get("applicationId");
7623
- const sessionDataKeyFromUrl = urlParams.get("sessionDataKey");
7624
- if (sessionDataKeyFromUrl) {
7625
- sessionStorage.setItem("asgardeo_session_data_key", sessionDataKeyFromUrl);
7626
- }
7627
- const effectiveApplicationId = applicationId || applicationIdFromUrl;
7628
- if (!flowIdFromUrl && !effectiveApplicationId) {
7687
+ const urlParams = getUrlParams();
7688
+ oauthCodeProcessedRef.current = false;
7689
+ handleSessionDataKey(urlParams.sessionDataKey);
7690
+ const effectiveApplicationId = applicationId || urlParams.applicationId;
7691
+ if (!urlParams.flowId && !effectiveApplicationId) {
7629
7692
  const error = new AsgardeoRuntimeError8(
7630
7693
  "Either flowId or applicationId is required for authentication",
7631
- "SignIn-initializeFlow-RuntimeError-001",
7632
- "react",
7633
- "Something went wrong while trying to sign in. Please try again later."
7694
+ "SIGN_IN_ERROR",
7695
+ "react"
7634
7696
  );
7635
- setFlowError(error);
7697
+ setError(error);
7636
7698
  throw error;
7637
7699
  }
7638
7700
  try {
7639
7701
  setFlowError(null);
7640
7702
  let response;
7641
- if (flowIdFromUrl) {
7703
+ if (urlParams.flowId) {
7642
7704
  response = await signIn({
7643
- flowId: flowIdFromUrl
7705
+ flowId: urlParams.flowId
7644
7706
  });
7645
7707
  } else {
7646
7708
  response = await signIn({
@@ -7653,20 +7715,23 @@ var SignIn = ({ className, size = "medium", onSuccess, onError, variant, childre
7653
7715
  }
7654
7716
  const { flowId, components: components2 } = normalizeFlowResponse(response, t);
7655
7717
  if (flowId && components2) {
7656
- setCurrentFlowId(flowId);
7718
+ setFlowId(flowId);
7657
7719
  setComponents(components2);
7658
7720
  setIsFlowInitialized(true);
7721
+ cleanupFlowUrlParams();
7659
7722
  }
7660
7723
  } catch (error) {
7661
7724
  const err = error;
7662
- setFlowError(err);
7663
- onError?.(err);
7664
- throw new AsgardeoRuntimeError8(
7665
- `Failed to initialize authentication flow: ${error instanceof Error ? error.message : String(error)}`,
7666
- "SignIn-initializeFlow-RuntimeError-002",
7667
- "react",
7668
- "Something went wrong while trying to sign in. Please try again later."
7725
+ clearFlowState();
7726
+ const errorMessage = err instanceof Error ? err.message : String(err);
7727
+ const displayError = new AsgardeoRuntimeError8(
7728
+ errorMessage,
7729
+ "SIGN_IN_ERROR",
7730
+ "react"
7669
7731
  );
7732
+ setError(displayError);
7733
+ initializationAttemptedRef.current = false;
7734
+ return;
7670
7735
  }
7671
7736
  };
7672
7737
  const handleSubmit = async (payload) => {
@@ -7689,81 +7754,99 @@ var SignIn = ({ className, size = "medium", onSuccess, onError, variant, childre
7689
7754
  return;
7690
7755
  }
7691
7756
  const { flowId, components: components2 } = normalizeFlowResponse(response, t);
7757
+ if (response.flowStatus === EmbeddedSignInFlowStatusV2.Error) {
7758
+ console.error("[SignIn] Flow returned Error status, clearing flow state");
7759
+ clearFlowState();
7760
+ const failureReason = response?.failureReason;
7761
+ const errorMessage = failureReason || "Authentication flow failed. Please try again.";
7762
+ const err = new AsgardeoRuntimeError8(
7763
+ errorMessage,
7764
+ "SIGN_IN_ERROR",
7765
+ "react"
7766
+ );
7767
+ setError(err);
7768
+ cleanupFlowUrlParams();
7769
+ return;
7770
+ }
7692
7771
  if (response.flowStatus === EmbeddedSignInFlowStatusV2.Complete) {
7693
- const redirectUrl = response.redirectUrl || response.redirect_uri;
7694
- sessionStorage.removeItem("asgardeo_flow_id");
7695
- if (redirectUrl) {
7696
- sessionStorage.removeItem("asgardeo_session_data_key");
7697
- }
7698
- const url = new URL(window.location.href);
7699
- url.searchParams.delete("code");
7700
- url.searchParams.delete("state");
7701
- url.searchParams.delete("nonce");
7702
- window.history.replaceState({}, "", url.toString());
7772
+ const redirectUrl = response?.redirectUrl || response?.redirect_uri;
7703
7773
  const finalRedirectUrl = redirectUrl || afterSignInUrl;
7774
+ setIsSubmitting(false);
7775
+ setFlowId(null);
7776
+ setIsFlowInitialized(false);
7777
+ sessionStorage.removeItem("asgardeo_flow_id");
7778
+ sessionStorage.removeItem("asgardeo_session_data_key");
7779
+ cleanupOAuthUrlParams(true);
7704
7780
  onSuccess && onSuccess({
7705
7781
  redirectUrl: finalRedirectUrl,
7706
- ...response.data
7782
+ ...response.data || {}
7707
7783
  });
7708
- if (finalRedirectUrl) {
7784
+ if (finalRedirectUrl && window?.location) {
7709
7785
  window.location.href = finalRedirectUrl;
7786
+ } else {
7787
+ console.warn("[SignIn] Flow completed but no redirect URL available");
7710
7788
  }
7711
7789
  return;
7712
7790
  }
7713
7791
  if (flowId && components2) {
7714
- setCurrentFlowId(flowId);
7792
+ setFlowId(flowId);
7715
7793
  setComponents(components2);
7716
- }
7717
- if (!currentFlowId && effectiveFlowId) {
7718
- setCurrentFlowId(effectiveFlowId);
7794
+ setIsFlowInitialized(true);
7795
+ cleanupFlowUrlParams();
7719
7796
  }
7720
7797
  } catch (error) {
7721
7798
  const err = error;
7722
- setFlowError(err);
7723
- onError?.(err);
7724
- throw new AsgardeoRuntimeError8(
7725
- `Failed to submit authentication flow: ${error instanceof Error ? error.message : String(error)}`,
7726
- "SignIn-handleSubmit-RuntimeError-001",
7727
- "react",
7728
- "Something went wrong while trying to sign in. Please try again later."
7799
+ clearFlowState();
7800
+ const errorMessage = err instanceof Error ? err.message : String(err);
7801
+ const displayError = new AsgardeoRuntimeError8(
7802
+ errorMessage,
7803
+ "SIGN_IN_ERROR",
7804
+ "react"
7729
7805
  );
7806
+ setError(displayError);
7807
+ return;
7730
7808
  } finally {
7731
7809
  setIsSubmitting(false);
7732
7810
  }
7733
7811
  };
7734
7812
  const handleError = (error) => {
7735
7813
  console.error("Authentication error:", error);
7736
- setFlowError(error);
7737
- onError?.(error);
7814
+ setError(error);
7738
7815
  };
7739
7816
  useEffect13(() => {
7740
- const urlParams = new URL(window.location.href).searchParams;
7741
- const code = urlParams.get("code");
7742
- const nonce = urlParams.get("nonce");
7743
- const state = urlParams.get("state");
7744
- const flowIdFromUrl = urlParams.get("flowId");
7817
+ const urlParams = getUrlParams();
7745
7818
  const storedFlowId = sessionStorage.getItem("asgardeo_flow_id");
7746
- if (!code || oauthCodeProcessedRef.current || isSubmitting) {
7819
+ if (urlParams.error) {
7820
+ handleOAuthError(urlParams.error, urlParams.errorDescription);
7821
+ oauthCodeProcessedRef.current = true;
7822
+ return;
7823
+ }
7824
+ if (!urlParams.code || oauthCodeProcessedRef.current || isSubmitting) {
7747
7825
  return;
7748
7826
  }
7749
- const flowIdToUse = currentFlowId || state || flowIdFromUrl || storedFlowId;
7827
+ const flowIdToUse = resolveFlowId(
7828
+ currentFlowId,
7829
+ urlParams.state,
7830
+ urlParams.flowId,
7831
+ storedFlowId
7832
+ );
7750
7833
  if (!flowIdToUse || !signIn) {
7751
7834
  return;
7752
7835
  }
7753
7836
  oauthCodeProcessedRef.current = true;
7754
7837
  if (!currentFlowId) {
7755
- setCurrentFlowId(flowIdToUse);
7756
- setIsFlowInitialized(true);
7838
+ setFlowId(flowIdToUse);
7757
7839
  }
7758
7840
  const submitPayload = {
7759
7841
  flowId: flowIdToUse,
7760
7842
  inputs: {
7761
- code,
7762
- ...nonce && { nonce }
7843
+ code: urlParams.code,
7844
+ ...urlParams.nonce && { nonce: urlParams.nonce }
7763
7845
  }
7764
7846
  };
7765
- handleSubmit(submitPayload).catch(() => {
7766
- oauthCodeProcessedRef.current = false;
7847
+ handleSubmit(submitPayload).catch((error) => {
7848
+ console.error("[SignIn] OAuth callback submission failed:", error);
7849
+ cleanupOAuthUrlParams(true);
7767
7850
  });
7768
7851
  }, [isFlowInitialized, currentFlowId, isInitialized, isLoading, isSubmitting, signIn]);
7769
7852
  if (children) {
@@ -8894,7 +8977,21 @@ var BaseSignUpContent = ({
8894
8977
  handleSubmit
8895
8978
  ]
8896
8979
  );
8980
+ const getUrlParams = () => {
8981
+ const urlParams = new URL(window?.location?.href ?? "").searchParams;
8982
+ return {
8983
+ code: urlParams.get("code"),
8984
+ state: urlParams.get("state"),
8985
+ error: urlParams.get("error")
8986
+ };
8987
+ };
8897
8988
  useEffect14(() => {
8989
+ if (platform === Platform5.AsgardeoV2) {
8990
+ const urlParams = getUrlParams();
8991
+ if (urlParams.code || urlParams.state) {
8992
+ return;
8993
+ }
8994
+ }
8898
8995
  if (isInitialized && !isFlowInitialized && !initializationAttemptedRef.current) {
8899
8996
  initializationAttemptedRef.current = true;
8900
8997
  (async () => {