@ory/elements-react 1.0.0-pr.5494d7c2 → 1.0.0-pr.97a7df82

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.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { UiNodeGroupEnum, isUiNodeInputAttributes, isUiNodeAnchorAttributes, isUiNodeImageAttributes, isUiNodeScriptAttributes, FlowType, getNodeId, Configuration, FrontendApi, isUiNodeTextAttributes, UiNodeInputAttributesTypeEnum, handleContinueWith, handleFlowError, settingsUrl, isResponseError, loginUrl, recoveryUrl, verificationUrl, registrationUrl, instanceOfContinueWithRecoveryUi } from '@ory/client-fetch';
1
+ import { UiNodeGroupEnum, isUiNodeInputAttributes, isUiNodeAnchorAttributes, isUiNodeImageAttributes, isUiNodeScriptAttributes, FlowType, getNodeId, Configuration, FrontendApi, isUiNodeTextAttributes, UiNodeInputAttributesTypeEnum, handleContinueWith, isResponseError as isResponseError$1, loginUrl, settingsUrl, registrationUrl, FetchError, ResponseError, recoveryUrl, instanceOfContinueWithRecoveryUi, verificationUrl as verificationUrl$1 } from '@ory/client-fetch';
2
2
  import { createContext, useContext, useRef, useState, useMemo, useReducer, useEffect } from 'react';
3
3
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
4
4
  import { useIntl, IntlProvider as IntlProvider$1 } from 'react-intl';
@@ -320,6 +320,8 @@ function parseStateFromFlow(flow) {
320
320
  return { current: "method_active", method: "code" };
321
321
  } else if (methodWithMessage) {
322
322
  return { current: "method_active", method: methodWithMessage.group };
323
+ } else if ((_a = flow.flow.ui.messages) == null ? void 0 : _a.some((m) => m.id === 1010016)) {
324
+ return { current: "select_method" };
323
325
  } else if (flow.flow.active && !["default", "identifier_first"].includes(flow.flow.active)) {
324
326
  return { current: "method_active", method: flow.flow.active };
325
327
  } else if (isChoosingMethod(flow)) {
@@ -328,8 +330,6 @@ function parseStateFromFlow(flow) {
328
330
  return { current: "method_active", method: authMethods[0] };
329
331
  }
330
332
  return { current: "select_method" };
331
- } else if ((_a = flow.flow.ui.messages) == null ? void 0 : _a.some((m) => m.id === 1010016)) {
332
- return { current: "select_method" };
333
333
  }
334
334
  return { current: "provide_identifier" };
335
335
  }
@@ -680,6 +680,134 @@ function replaceWindowFlowId(flow) {
680
680
  url.searchParams.set("flow", flow);
681
681
  window.location.href = url.toString();
682
682
  }
683
+ function isGenericErrorResponse(response) {
684
+ return typeof response === "object" && !!response && "error" in response && typeof response.error === "object" && !!response.error && "id" in response.error;
685
+ }
686
+ function isNeedsPrivilegedSessionError(response) {
687
+ return isGenericErrorResponse(response) && response.error.id === "session_refresh_required";
688
+ }
689
+ function isSelfServiceFlowExpiredError(response) {
690
+ return isGenericErrorResponse(response) && response.error.id === "self_service_flow_expired";
691
+ }
692
+ function isBrowserLocationChangeRequired(response) {
693
+ return isGenericErrorResponse(response) && isGenericErrorResponse(response) && response.error.id === "browser_location_change_required";
694
+ }
695
+ function isAddressNotVerified(response) {
696
+ return isGenericErrorResponse(response) && response.error.id === "session_verified_address_required";
697
+ }
698
+ function isCsrfError(response) {
699
+ return isGenericErrorResponse(response) && response.error.id === "security_csrf_violation";
700
+ }
701
+ var isResponseError = (err) => {
702
+ if (err instanceof ResponseError) {
703
+ return true;
704
+ }
705
+ return typeof err === "object" && !!err && "name" in err && err.name === "ResponseError";
706
+ };
707
+ var isFetchError = (err) => {
708
+ return err instanceof FetchError;
709
+ };
710
+
711
+ // src/util/sdk-helpers/urlHelpers.ts
712
+ var verificationUrl = (config) => config.sdk.url + "/self-service/verification/browser";
713
+ var handleFlowError = (opts) => async (err) => {
714
+ var _a;
715
+ if (!isResponseError(err)) {
716
+ if (isFetchError(err)) {
717
+ throw new FetchError(
718
+ err,
719
+ "Unable to call the API endpoint. Ensure that CORS is set up correctly and that you have provided a valid SDK URL to Ory Elements."
720
+ );
721
+ }
722
+ throw err;
723
+ }
724
+ const contentType = err.response.headers.get("content-type") || "";
725
+ if (contentType.includes("application/json")) {
726
+ const body = await toBody(err.response);
727
+ if (isSelfServiceFlowExpiredError(body)) {
728
+ opts.onRestartFlow(body.use_flow_id);
729
+ return;
730
+ } else if (isAddressNotVerified(body)) {
731
+ for (const continueWith of ((_a = body.error.details) == null ? void 0 : _a.continue_with) || []) {
732
+ if (continueWith.action === "show_verification_ui" && continueWith.flow.url) {
733
+ opts.onRedirect(continueWith.flow.url, true);
734
+ return;
735
+ }
736
+ }
737
+ opts.onRedirect(verificationUrl(opts.config), true);
738
+ return;
739
+ } else if (isBrowserLocationChangeRequired(body) && body.redirect_browser_to) {
740
+ opts.onRedirect(body.redirect_browser_to, true);
741
+ return;
742
+ } else if (isNeedsPrivilegedSessionError(body) && body.redirect_browser_to) {
743
+ opts.onRedirect(body.redirect_browser_to, true);
744
+ return;
745
+ } else if (isCsrfError(body)) {
746
+ opts.onRestartFlow();
747
+ return;
748
+ }
749
+ switch (err.response.status) {
750
+ case 404:
751
+ opts.onRestartFlow();
752
+ return;
753
+ case 410:
754
+ opts.onRestartFlow();
755
+ return;
756
+ case 400:
757
+ return opts.onValidationError(
758
+ await err.response.json()
759
+ );
760
+ case 403:
761
+ opts.onRestartFlow();
762
+ return;
763
+ case 422: {
764
+ throw new ResponseError(
765
+ err.response,
766
+ "The API returned an error code indicating a required redirect, but the SDK is outdated and does not know how to handle the action. Received response: " + await err.response.json()
767
+ );
768
+ }
769
+ }
770
+ throw new ResponseError(
771
+ err.response,
772
+ "The Ory API endpoint returned a response code the SDK does not know how to handle. Please check the network tab for more information. Received response: " + await err.response.json()
773
+ );
774
+ } else if (
775
+ // Not a JSON response? If it's a text response we will return an error informing the user that the response is not JSON.
776
+ contentType.includes("text/") || contentType.includes("html") || contentType.includes("xml")
777
+ ) {
778
+ await logResponseError(err.response, true);
779
+ throw new ResponseError(
780
+ err.response,
781
+ `The Ory API endpoint returned an unexpected HTML or text response. Check your console output for details.`
782
+ );
783
+ }
784
+ await logResponseError(err.response, false);
785
+ throw new ResponseError(
786
+ err.response,
787
+ "The Ory API endpoint returned unexpected content type `" + contentType + "`. Check your console output for details."
788
+ );
789
+ };
790
+ async function toBody(response) {
791
+ try {
792
+ return await response.clone().json();
793
+ } catch (e) {
794
+ await logResponseError(response, true, [e]);
795
+ throw new ResponseError(
796
+ response,
797
+ "Unable to decode API response using JSON."
798
+ );
799
+ }
800
+ }
801
+ async function logResponseError(response, printBody, wrap) {
802
+ console.error("Unable to decode API response", {
803
+ response: {
804
+ status: response.status,
805
+ headers: Object.fromEntries(response.headers.entries()),
806
+ body: printBody ? await response.clone().text() : void 0
807
+ },
808
+ errors: wrap
809
+ });
810
+ }
683
811
 
684
812
  // src/util/onSubmitLogin.ts
685
813
  async function onSubmitLogin({ flow }, config, {
@@ -715,7 +843,8 @@ async function onSubmitLogin({ flow }, config, {
715
843
  flowType: FlowType.Login
716
844
  });
717
845
  },
718
- onRedirect
846
+ onRedirect,
847
+ config
719
848
  })
720
849
  );
721
850
  }
@@ -759,7 +888,8 @@ async function onSubmitRecovery({ flow }, config, {
759
888
  });
760
889
  }
761
890
  },
762
- onRedirect
891
+ onRedirect,
892
+ config
763
893
  })
764
894
  );
765
895
  }
@@ -808,7 +938,8 @@ async function onSubmitRegistration({ flow }, config, {
808
938
  flowType: FlowType.Registration
809
939
  });
810
940
  },
811
- onRedirect
941
+ onRedirect,
942
+ config
812
943
  })
813
944
  );
814
945
  }
@@ -847,10 +978,11 @@ async function onSubmitSettings({ flow }, config, {
847
978
  flowType: FlowType.Settings
848
979
  });
849
980
  },
850
- onRedirect
981
+ onRedirect,
982
+ config
851
983
  })
852
984
  ).catch((err) => {
853
- if (isResponseError(err)) {
985
+ if (isResponseError$1(err)) {
854
986
  if (err.response.status === 401) {
855
987
  return onRedirect(
856
988
  loginUrl(config) + "?return_to=" + settingsUrl(config),
@@ -880,7 +1012,7 @@ async function onSubmitVerification({ flow }, config, {
880
1012
  if (useFlowId) {
881
1013
  replaceWindowFlowId(useFlowId);
882
1014
  } else {
883
- onRedirect(verificationUrl(config), true);
1015
+ onRedirect(verificationUrl$1(config), true);
884
1016
  }
885
1017
  },
886
1018
  onValidationError: (body2) => {
@@ -889,7 +1021,8 @@ async function onSubmitVerification({ flow }, config, {
889
1021
  flowType: FlowType.Verification
890
1022
  });
891
1023
  },
892
- onRedirect
1024
+ onRedirect,
1025
+ config
893
1026
  })
894
1027
  );
895
1028
  }