@asgardeo/react 0.6.8 → 0.6.10

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