@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/cjs/index.js CHANGED
@@ -7643,18 +7643,87 @@ var SignIn = ({ className, size = "medium", onSuccess, onError, variant, childre
7643
7643
  const [isSubmitting, setIsSubmitting] = (0, import_react59.useState)(false);
7644
7644
  const initializationAttemptedRef = (0, import_react59.useRef)(false);
7645
7645
  const oauthCodeProcessedRef = (0, import_react59.useRef)(false);
7646
+ const setFlowId = (flowId) => {
7647
+ setCurrentFlowId(flowId);
7648
+ if (flowId) {
7649
+ sessionStorage.setItem("asgardeo_flow_id", flowId);
7650
+ } else {
7651
+ sessionStorage.removeItem("asgardeo_flow_id");
7652
+ }
7653
+ };
7654
+ const clearFlowState = () => {
7655
+ setFlowId(null);
7656
+ setIsFlowInitialized(false);
7657
+ sessionStorage.removeItem("asgardeo_session_data_key");
7658
+ oauthCodeProcessedRef.current = false;
7659
+ };
7660
+ const getUrlParams = () => {
7661
+ const urlParams = new URL(window?.location?.href ?? "").searchParams;
7662
+ return {
7663
+ code: urlParams.get("code"),
7664
+ error: urlParams.get("error"),
7665
+ errorDescription: urlParams.get("error_description"),
7666
+ state: urlParams.get("state"),
7667
+ nonce: urlParams.get("nonce"),
7668
+ flowId: urlParams.get("flowId"),
7669
+ applicationId: urlParams.get("applicationId"),
7670
+ sessionDataKey: urlParams.get("sessionDataKey")
7671
+ };
7672
+ };
7673
+ const handleSessionDataKey = (sessionDataKey) => {
7674
+ if (sessionDataKey) {
7675
+ sessionStorage.setItem("asgardeo_session_data_key", sessionDataKey);
7676
+ }
7677
+ };
7678
+ const resolveFlowId = (currentFlowId2, state, flowIdFromUrl, storedFlowId) => {
7679
+ return currentFlowId2 || state || flowIdFromUrl || storedFlowId || null;
7680
+ };
7681
+ const cleanupOAuthUrlParams = (includeNonce = false) => {
7682
+ if (!window?.location?.href) return;
7683
+ const url = new URL(window.location.href);
7684
+ url.searchParams.delete("error");
7685
+ url.searchParams.delete("error_description");
7686
+ url.searchParams.delete("code");
7687
+ url.searchParams.delete("state");
7688
+ if (includeNonce) {
7689
+ url.searchParams.delete("nonce");
7690
+ }
7691
+ window?.history?.replaceState({}, "", url.toString());
7692
+ };
7693
+ const cleanupFlowUrlParams = () => {
7694
+ if (!window?.location?.href) return;
7695
+ const url = new URL(window.location.href);
7696
+ url.searchParams.delete("flowId");
7697
+ url.searchParams.delete("sessionDataKey");
7698
+ url.searchParams.delete("applicationId");
7699
+ window?.history?.replaceState({}, "", url.toString());
7700
+ };
7701
+ const handleOAuthError = (error, errorDescription) => {
7702
+ console.warn("[SignIn] OAuth error detected:", error);
7703
+ clearFlowState();
7704
+ const errorMessage = errorDescription || `OAuth error: ${error}`;
7705
+ const err = new import_browser51.AsgardeoRuntimeError(
7706
+ errorMessage,
7707
+ "SIGN_IN_ERROR",
7708
+ "react"
7709
+ );
7710
+ setError(err);
7711
+ cleanupOAuthUrlParams(true);
7712
+ };
7713
+ const setError = (error) => {
7714
+ setFlowError(error);
7715
+ setIsFlowInitialized(true);
7716
+ onError?.(error);
7717
+ };
7646
7718
  const handleRedirection = (response) => {
7647
7719
  if (response.type === import_browser51.EmbeddedSignInFlowTypeV2.Redirection) {
7648
7720
  const redirectURL = response.data?.redirectURL || response?.redirectURL;
7649
- if (redirectURL) {
7721
+ if (redirectURL && window?.location) {
7650
7722
  if (response.flowId) {
7651
- sessionStorage.setItem("asgardeo_flow_id", response.flowId);
7652
- }
7653
- const urlParams = new URL(window.location.href).searchParams;
7654
- const sessionDataKeyFromUrl = urlParams.get("sessionDataKey");
7655
- if (sessionDataKeyFromUrl) {
7656
- sessionStorage.setItem("asgardeo_session_data_key", sessionDataKeyFromUrl);
7723
+ setFlowId(response.flowId);
7657
7724
  }
7725
+ const urlParams = getUrlParams();
7726
+ handleSessionDataKey(urlParams.sessionDataKey);
7658
7727
  window.location.href = redirectURL;
7659
7728
  return true;
7660
7729
  }
@@ -7663,58 +7732,41 @@ var SignIn = ({ className, size = "medium", onSuccess, onError, variant, childre
7663
7732
  };
7664
7733
  (0, import_react59.useEffect)(() => {
7665
7734
  const storedFlowId = sessionStorage.getItem("asgardeo_flow_id");
7666
- const urlParams = new URL(window.location.href).searchParams;
7667
- const code = urlParams.get("code");
7668
- const state = urlParams.get("state");
7669
- const sessionDataKeyFromUrl = urlParams.get("sessionDataKey");
7670
- if (sessionDataKeyFromUrl) {
7671
- sessionStorage.setItem("asgardeo_session_data_key", sessionDataKeyFromUrl);
7672
- }
7673
- if (code) {
7674
- const flowIdFromUrl = urlParams.get("flowId");
7675
- const flowIdFromState = state || flowIdFromUrl || storedFlowId;
7676
- if (flowIdFromState) {
7677
- setCurrentFlowId(flowIdFromState);
7678
- setIsFlowInitialized(true);
7679
- sessionStorage.setItem("asgardeo_flow_id", flowIdFromState);
7680
- initializationAttemptedRef.current = true;
7681
- }
7735
+ const urlParams = getUrlParams();
7736
+ if (urlParams.error) {
7737
+ handleOAuthError(urlParams.error, urlParams.errorDescription);
7738
+ return;
7739
+ }
7740
+ handleSessionDataKey(urlParams.sessionDataKey);
7741
+ if (urlParams.code || urlParams.state) {
7682
7742
  return;
7683
7743
  }
7684
- if (isInitialized && !isLoading && !isFlowInitialized && !initializationAttemptedRef.current && !currentFlowId) {
7744
+ const currentUrlParams = getUrlParams();
7745
+ if (isInitialized && !isLoading && !isFlowInitialized && !initializationAttemptedRef.current && !currentFlowId && !currentUrlParams.code && !currentUrlParams.state && !isSubmitting && !oauthCodeProcessedRef.current) {
7685
7746
  initializationAttemptedRef.current = true;
7686
7747
  initializeFlow();
7687
7748
  }
7688
7749
  }, [isInitialized, isLoading, isFlowInitialized, currentFlowId]);
7689
7750
  const initializeFlow = async () => {
7690
- const urlParams = new URL(window.location.href).searchParams;
7691
- const code = urlParams.get("code");
7692
- if (code) {
7693
- return;
7694
- }
7695
- const flowIdFromUrl = urlParams.get("flowId");
7696
- const applicationIdFromUrl = urlParams.get("applicationId");
7697
- const sessionDataKeyFromUrl = urlParams.get("sessionDataKey");
7698
- if (sessionDataKeyFromUrl) {
7699
- sessionStorage.setItem("asgardeo_session_data_key", sessionDataKeyFromUrl);
7700
- }
7701
- const effectiveApplicationId = applicationId || applicationIdFromUrl;
7702
- if (!flowIdFromUrl && !effectiveApplicationId) {
7751
+ const urlParams = getUrlParams();
7752
+ oauthCodeProcessedRef.current = false;
7753
+ handleSessionDataKey(urlParams.sessionDataKey);
7754
+ const effectiveApplicationId = applicationId || urlParams.applicationId;
7755
+ if (!urlParams.flowId && !effectiveApplicationId) {
7703
7756
  const error = new import_browser51.AsgardeoRuntimeError(
7704
7757
  "Either flowId or applicationId is required for authentication",
7705
- "SignIn-initializeFlow-RuntimeError-001",
7706
- "react",
7707
- "Something went wrong while trying to sign in. Please try again later."
7758
+ "SIGN_IN_ERROR",
7759
+ "react"
7708
7760
  );
7709
- setFlowError(error);
7761
+ setError(error);
7710
7762
  throw error;
7711
7763
  }
7712
7764
  try {
7713
7765
  setFlowError(null);
7714
7766
  let response;
7715
- if (flowIdFromUrl) {
7767
+ if (urlParams.flowId) {
7716
7768
  response = await signIn({
7717
- flowId: flowIdFromUrl
7769
+ flowId: urlParams.flowId
7718
7770
  });
7719
7771
  } else {
7720
7772
  response = await signIn({
@@ -7727,20 +7779,23 @@ var SignIn = ({ className, size = "medium", onSuccess, onError, variant, childre
7727
7779
  }
7728
7780
  const { flowId, components: components2 } = normalizeFlowResponse(response, t);
7729
7781
  if (flowId && components2) {
7730
- setCurrentFlowId(flowId);
7782
+ setFlowId(flowId);
7731
7783
  setComponents(components2);
7732
7784
  setIsFlowInitialized(true);
7785
+ cleanupFlowUrlParams();
7733
7786
  }
7734
7787
  } catch (error) {
7735
7788
  const err = error;
7736
- setFlowError(err);
7737
- onError?.(err);
7738
- throw new import_browser51.AsgardeoRuntimeError(
7739
- `Failed to initialize authentication flow: ${error instanceof Error ? error.message : String(error)}`,
7740
- "SignIn-initializeFlow-RuntimeError-002",
7741
- "react",
7742
- "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 import_browser51.AsgardeoRuntimeError(
7792
+ errorMessage,
7793
+ "SIGN_IN_ERROR",
7794
+ "react"
7743
7795
  );
7796
+ setError(displayError);
7797
+ initializationAttemptedRef.current = false;
7798
+ return;
7744
7799
  }
7745
7800
  };
7746
7801
  const handleSubmit = async (payload) => {
@@ -7763,81 +7818,99 @@ var SignIn = ({ className, size = "medium", onSuccess, onError, variant, childre
7763
7818
  return;
7764
7819
  }
7765
7820
  const { flowId, components: components2 } = normalizeFlowResponse(response, t);
7821
+ if (response.flowStatus === import_browser51.EmbeddedSignInFlowStatusV2.Error) {
7822
+ console.error("[SignIn] Flow returned Error status, clearing flow state");
7823
+ clearFlowState();
7824
+ const failureReason = response?.failureReason;
7825
+ const errorMessage = failureReason || "Authentication flow failed. Please try again.";
7826
+ const err = new import_browser51.AsgardeoRuntimeError(
7827
+ errorMessage,
7828
+ "SIGN_IN_ERROR",
7829
+ "react"
7830
+ );
7831
+ setError(err);
7832
+ cleanupFlowUrlParams();
7833
+ return;
7834
+ }
7766
7835
  if (response.flowStatus === import_browser51.EmbeddedSignInFlowStatusV2.Complete) {
7767
- const redirectUrl = response.redirectUrl || response.redirect_uri;
7768
- sessionStorage.removeItem("asgardeo_flow_id");
7769
- if (redirectUrl) {
7770
- sessionStorage.removeItem("asgardeo_session_data_key");
7771
- }
7772
- const url = new URL(window.location.href);
7773
- url.searchParams.delete("code");
7774
- url.searchParams.delete("state");
7775
- url.searchParams.delete("nonce");
7776
- window.history.replaceState({}, "", url.toString());
7836
+ const redirectUrl = response?.redirectUrl || response?.redirect_uri;
7777
7837
  const finalRedirectUrl = redirectUrl || afterSignInUrl;
7838
+ setIsSubmitting(false);
7839
+ setFlowId(null);
7840
+ setIsFlowInitialized(false);
7841
+ sessionStorage.removeItem("asgardeo_flow_id");
7842
+ sessionStorage.removeItem("asgardeo_session_data_key");
7843
+ cleanupOAuthUrlParams(true);
7778
7844
  onSuccess && onSuccess({
7779
7845
  redirectUrl: finalRedirectUrl,
7780
- ...response.data
7846
+ ...response.data || {}
7781
7847
  });
7782
- if (finalRedirectUrl) {
7848
+ if (finalRedirectUrl && window?.location) {
7783
7849
  window.location.href = finalRedirectUrl;
7850
+ } else {
7851
+ console.warn("[SignIn] Flow completed but no redirect URL available");
7784
7852
  }
7785
7853
  return;
7786
7854
  }
7787
7855
  if (flowId && components2) {
7788
- setCurrentFlowId(flowId);
7856
+ setFlowId(flowId);
7789
7857
  setComponents(components2);
7790
- }
7791
- if (!currentFlowId && effectiveFlowId) {
7792
- setCurrentFlowId(effectiveFlowId);
7858
+ setIsFlowInitialized(true);
7859
+ cleanupFlowUrlParams();
7793
7860
  }
7794
7861
  } catch (error) {
7795
7862
  const err = error;
7796
- setFlowError(err);
7797
- onError?.(err);
7798
- throw new import_browser51.AsgardeoRuntimeError(
7799
- `Failed to submit authentication flow: ${error instanceof Error ? error.message : String(error)}`,
7800
- "SignIn-handleSubmit-RuntimeError-001",
7801
- "react",
7802
- "Something went wrong while trying to sign in. Please try again later."
7863
+ clearFlowState();
7864
+ const errorMessage = err instanceof Error ? err.message : String(err);
7865
+ const displayError = new import_browser51.AsgardeoRuntimeError(
7866
+ errorMessage,
7867
+ "SIGN_IN_ERROR",
7868
+ "react"
7803
7869
  );
7870
+ setError(displayError);
7871
+ return;
7804
7872
  } finally {
7805
7873
  setIsSubmitting(false);
7806
7874
  }
7807
7875
  };
7808
7876
  const handleError = (error) => {
7809
7877
  console.error("Authentication error:", error);
7810
- setFlowError(error);
7811
- onError?.(error);
7878
+ setError(error);
7812
7879
  };
7813
7880
  (0, import_react59.useEffect)(() => {
7814
- const urlParams = new URL(window.location.href).searchParams;
7815
- const code = urlParams.get("code");
7816
- const nonce = urlParams.get("nonce");
7817
- const state = urlParams.get("state");
7818
- const flowIdFromUrl = urlParams.get("flowId");
7881
+ const urlParams = getUrlParams();
7819
7882
  const storedFlowId = sessionStorage.getItem("asgardeo_flow_id");
7820
- if (!code || oauthCodeProcessedRef.current || isSubmitting) {
7883
+ if (urlParams.error) {
7884
+ handleOAuthError(urlParams.error, urlParams.errorDescription);
7885
+ oauthCodeProcessedRef.current = true;
7886
+ return;
7887
+ }
7888
+ if (!urlParams.code || oauthCodeProcessedRef.current || isSubmitting) {
7821
7889
  return;
7822
7890
  }
7823
- const flowIdToUse = currentFlowId || state || flowIdFromUrl || storedFlowId;
7891
+ const flowIdToUse = resolveFlowId(
7892
+ currentFlowId,
7893
+ urlParams.state,
7894
+ urlParams.flowId,
7895
+ storedFlowId
7896
+ );
7824
7897
  if (!flowIdToUse || !signIn) {
7825
7898
  return;
7826
7899
  }
7827
7900
  oauthCodeProcessedRef.current = true;
7828
7901
  if (!currentFlowId) {
7829
- setCurrentFlowId(flowIdToUse);
7830
- setIsFlowInitialized(true);
7902
+ setFlowId(flowIdToUse);
7831
7903
  }
7832
7904
  const submitPayload = {
7833
7905
  flowId: flowIdToUse,
7834
7906
  inputs: {
7835
- code,
7836
- ...nonce && { nonce }
7907
+ code: urlParams.code,
7908
+ ...urlParams.nonce && { nonce: urlParams.nonce }
7837
7909
  }
7838
7910
  };
7839
- handleSubmit(submitPayload).catch(() => {
7840
- oauthCodeProcessedRef.current = false;
7911
+ handleSubmit(submitPayload).catch((error) => {
7912
+ console.error("[SignIn] OAuth callback submission failed:", error);
7913
+ cleanupOAuthUrlParams(true);
7841
7914
  });
7842
7915
  }, [isFlowInitialized, currentFlowId, isInitialized, isLoading, isSubmitting, signIn]);
7843
7916
  if (children) {
@@ -8962,7 +9035,21 @@ var BaseSignUpContent = ({
8962
9035
  handleSubmit
8963
9036
  ]
8964
9037
  );
9038
+ const getUrlParams = () => {
9039
+ const urlParams = new URL(window?.location?.href ?? "").searchParams;
9040
+ return {
9041
+ code: urlParams.get("code"),
9042
+ state: urlParams.get("state"),
9043
+ error: urlParams.get("error")
9044
+ };
9045
+ };
8965
9046
  (0, import_react61.useEffect)(() => {
9047
+ if (platform === import_browser61.Platform.AsgardeoV2) {
9048
+ const urlParams = getUrlParams();
9049
+ if (urlParams.code || urlParams.state) {
9050
+ return;
9051
+ }
9052
+ }
8966
9053
  if (isInitialized && !isFlowInitialized && !initializationAttemptedRef.current) {
8967
9054
  initializationAttemptedRef.current = true;
8968
9055
  (async () => {