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