@ory/elements-react 0.0.0-pr.6ecad3e0 → 0.0.0-pr.75b46aac

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,5 +1,5 @@
1
- import { UiNodeGroupEnum, isUiNodeInputAttributes, isUiNodeAnchorAttributes, isUiNodeImageAttributes, isUiNodeScriptAttributes, FlowType, getNodeId, isUiNodeTextAttributes, UiNodeInputAttributesTypeEnum, handleContinueWith, handleFlowError, settingsUrl, isResponseError, loginUrl, recoveryUrl, verificationUrl, registrationUrl, Configuration, FrontendApi, instanceOfContinueWithRecoveryUi } from '@ory/client-fetch';
2
- import { createContext, useContext, useState, useMemo, useReducer, useRef, useEffect } from 'react';
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';
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';
5
5
  import { useFormContext, useForm, FormProvider } from 'react-hook-form';
@@ -368,6 +368,11 @@ function useFormStateReducer(flow) {
368
368
  setSelectedMethod(action2.method);
369
369
  return { current: "method_active", method: action2.method };
370
370
  }
371
+ case "action_method_selector": {
372
+ return {
373
+ current: "select_method"
374
+ };
375
+ }
371
376
  }
372
377
  return state;
373
378
  };
@@ -406,6 +411,110 @@ function OryFlowProvider({
406
411
  }
407
412
  );
408
413
  }
414
+
415
+ // src/client/config.ts
416
+ function isProduction() {
417
+ var _a, _b;
418
+ return ["production", "prod"].indexOf(
419
+ (_b = (_a = process.env.VERCEL_ENV) != null ? _a : process.env.NODE_ENV) != null ? _b : ""
420
+ ) > -1;
421
+ }
422
+ function frontendClient(sdkUrl, opts = {}) {
423
+ const config = new Configuration({
424
+ ...opts,
425
+ basePath: sdkUrl,
426
+ credentials: "include",
427
+ headers: {
428
+ Accept: "application/json",
429
+ ...opts.headers
430
+ }
431
+ });
432
+ return new FrontendApi(config);
433
+ }
434
+ var defaultProject = {
435
+ name: "Ory",
436
+ registration_enabled: true,
437
+ verification_enabled: true,
438
+ recovery_enabled: true,
439
+ recovery_ui_url: "/ui/recovery",
440
+ registration_ui_url: "/ui/registration",
441
+ verification_ui_url: "/ui/verification",
442
+ login_ui_url: "/ui/login",
443
+ settings_ui_url: "/ui/settings",
444
+ default_redirect_url: "/ui/welcome",
445
+ error_ui_url: "/ui/error",
446
+ default_locale: "en",
447
+ locale_behavior: "force_default"
448
+ };
449
+ function useOryConfiguration() {
450
+ const configCtx = useContext(OryConfigurationContext);
451
+ return {
452
+ sdk: {
453
+ ...configCtx.sdk,
454
+ frontend: frontendClient(configCtx.sdk.url, configCtx.sdk.options)
455
+ },
456
+ project: {
457
+ ...configCtx.project
458
+ }
459
+ };
460
+ }
461
+ var OryConfigurationContext = createContext({
462
+ sdk: computeSdkConfig({}),
463
+ project: defaultProject
464
+ });
465
+ function OryConfigurationProvider({
466
+ children,
467
+ sdk: initialConfig,
468
+ project
469
+ }) {
470
+ const configRef = useRef({
471
+ sdk: computeSdkConfig(initialConfig),
472
+ project: {
473
+ ...defaultProject,
474
+ ...project
475
+ }
476
+ });
477
+ return /* @__PURE__ */ jsx(OryConfigurationContext.Provider, { value: configRef.current, children });
478
+ }
479
+ function computeSdkConfig(config) {
480
+ if ((config == null ? void 0 : config.url) && typeof config.url === "string") {
481
+ console.debug("Using sdk url from config");
482
+ return {
483
+ url: config.url.replace(/\/$/, ""),
484
+ options: config.options || {}
485
+ };
486
+ }
487
+ return {
488
+ url: getSDKUrl(),
489
+ options: (config == null ? void 0 : config.options) || {}
490
+ };
491
+ }
492
+ function getSDKUrl() {
493
+ var _a;
494
+ if (typeof process !== "undefined" && process.versions && process.versions.node) {
495
+ if (isProduction()) {
496
+ const sdkUrl = (_a = process.env["NEXT_PUBLIC_ORY_SDK_URL"]) != null ? _a : process.env["ORY_SDK_URL"];
497
+ if (!sdkUrl) {
498
+ throw new Error(
499
+ "Unable to determine SDK URL. Please set NEXT_PUBLIC_ORY_SDK_URL and/or ORY_SDK_URL in production environments."
500
+ );
501
+ }
502
+ return sdkUrl.replace(/\/$/, "");
503
+ } else {
504
+ if (process.env["__NEXT_PRIVATE_ORIGIN"]) {
505
+ return process.env["__NEXT_PRIVATE_ORIGIN"].replace(/\/$/, "");
506
+ } else if (process.env["VERCEL_URL"]) {
507
+ return `https://${process.env["VERCEL_URL"]}`.replace(/\/$/, "");
508
+ }
509
+ }
510
+ }
511
+ if (typeof window !== "undefined") {
512
+ return window.location.origin;
513
+ }
514
+ throw new Error(
515
+ "Unable to determine SDK URL. Please set NEXT_PUBLIC_ORY_SDK_URL and/or ORY_SDK_URL or supply the sdk.url parameter in the Ory configuration."
516
+ );
517
+ }
409
518
  function mergeTranslations(customTranslations) {
410
519
  return Object.keys(customTranslations).reduce((acc, key) => {
411
520
  acc[key] = { ...OryLocales[key], ...customTranslations[key] };
@@ -435,17 +544,18 @@ var IntlProvider = ({
435
544
  function OryProvider({
436
545
  children,
437
546
  components: Components,
547
+ config,
438
548
  ...oryFlowProps
439
549
  }) {
440
550
  var _a, _b, _c;
441
- return /* @__PURE__ */ jsx(
551
+ return /* @__PURE__ */ jsx(OryConfigurationProvider, { sdk: config.sdk, project: config.project, children: /* @__PURE__ */ jsx(
442
552
  IntlProvider,
443
553
  {
444
- locale: (_b = (_a = oryFlowProps.config.intl) == null ? void 0 : _a.locale) != null ? _b : "en",
445
- customTranslations: (_c = oryFlowProps.config.intl) == null ? void 0 : _c.customTranslations,
554
+ locale: (_b = (_a = config.intl) == null ? void 0 : _a.locale) != null ? _b : "en",
555
+ customTranslations: (_c = config.intl) == null ? void 0 : _c.customTranslations,
446
556
  children: /* @__PURE__ */ jsx(OryFlowProvider, { ...oryFlowProps, children: /* @__PURE__ */ jsx(OryComponentProvider, { components: Components, children }) })
447
557
  }
448
- );
558
+ ) });
449
559
  }
450
560
  function OryCardHeader() {
451
561
  const { Card } = useComponents();
@@ -564,18 +674,6 @@ function OryCardContent({ children }) {
564
674
  const { Card } = useComponents();
565
675
  return /* @__PURE__ */ jsx(Card.Content, { children });
566
676
  }
567
- function frontendClient(sdkUrl, opts = {}) {
568
- const config = new Configuration({
569
- ...opts,
570
- basePath: sdkUrl,
571
- credentials: "include",
572
- headers: {
573
- Accept: "application/json",
574
- ...opts.headers
575
- }
576
- });
577
- return new FrontendApi(config);
578
- }
579
677
 
580
678
  // src/util/internal.ts
581
679
  function replaceWindowFlowId(flow) {
@@ -585,7 +683,7 @@ function replaceWindowFlowId(flow) {
585
683
  }
586
684
 
587
685
  // src/util/onSubmitLogin.ts
588
- async function onSubmitLogin({ config, flow }, {
686
+ async function onSubmitLogin({ flow }, config, {
589
687
  setFlowContainer,
590
688
  body,
591
689
  onRedirect
@@ -614,7 +712,6 @@ async function onSubmitLogin({ config, flow }, {
614
712
  },
615
713
  onValidationError: (body2) => {
616
714
  setFlowContainer({
617
- config,
618
715
  flow: body2,
619
716
  flowType: FlowType.Login
620
717
  });
@@ -623,18 +720,12 @@ async function onSubmitLogin({ config, flow }, {
623
720
  })
624
721
  );
625
722
  }
626
- async function onSubmitRecovery({ config, flow }, {
723
+ async function onSubmitRecovery({ flow }, config, {
627
724
  setFlowContainer,
628
725
  body,
629
726
  onRedirect
630
727
  }) {
631
- var _a;
632
- if (!config.sdk.url) {
633
- throw new Error(
634
- `Please supply your Ory Network SDK url to the Ory Elements configuration.`
635
- );
636
- }
637
- await frontendClient(config.sdk.url, (_a = config.sdk.options) != null ? _a : {}).updateRecoveryFlowRaw({
728
+ await config.sdk.frontend.updateRecoveryFlowRaw({
638
729
  flow: flow.id,
639
730
  updateRecoveryFlowBody: body
640
731
  }).then(async (res) => {
@@ -647,8 +738,7 @@ async function onSubmitRecovery({ config, flow }, {
647
738
  }
648
739
  setFlowContainer({
649
740
  flow: flow2,
650
- flowType: FlowType.Recovery,
651
- config
741
+ flowType: FlowType.Recovery
652
742
  });
653
743
  }).catch(
654
744
  handleFlowError({
@@ -666,8 +756,7 @@ async function onSubmitRecovery({ config, flow }, {
666
756
  } else {
667
757
  setFlowContainer({
668
758
  flow: body2,
669
- flowType: FlowType.Recovery,
670
- config
759
+ flowType: FlowType.Recovery
671
760
  });
672
761
  }
673
762
  },
@@ -688,19 +777,12 @@ function handleContinueWithRecoveryUIError(error, config, onRedirect) {
688
777
  }
689
778
  onRedirect(recoveryUrl(config), true);
690
779
  }
691
- async function onSubmitRegistration({ config, flow }, {
780
+ async function onSubmitRegistration({ flow }, config, {
692
781
  setFlowContainer,
693
782
  body,
694
783
  onRedirect
695
784
  }) {
696
- var _a;
697
- if (!config.sdk.url) {
698
- throw new Error(
699
- `Please supply your Ory Network SDK url to the Ory Elements configuration.`
700
- );
701
- }
702
- const client = frontendClient(config.sdk.url, (_a = config.sdk.options) != null ? _a : {});
703
- await client.updateRegistrationFlowRaw({
785
+ await config.sdk.frontend.updateRegistrationFlowRaw({
704
786
  flow: flow.id,
705
787
  updateRegistrationFlowBody: body
706
788
  }).then(async (res) => {
@@ -724,27 +806,19 @@ async function onSubmitRegistration({ config, flow }, {
724
806
  onValidationError: (body2) => {
725
807
  setFlowContainer({
726
808
  flow: body2,
727
- flowType: FlowType.Registration,
728
- config
809
+ flowType: FlowType.Registration
729
810
  });
730
811
  },
731
812
  onRedirect
732
813
  })
733
814
  );
734
815
  }
735
- async function onSubmitSettings({ config, flow }, {
816
+ async function onSubmitSettings({ flow }, config, {
736
817
  setFlowContainer,
737
818
  body,
738
819
  onRedirect
739
820
  }) {
740
- var _a;
741
- if (!config.sdk.url) {
742
- throw new Error(
743
- `Please supply your Ory Network SDK url to the Ory Elements configuration.`
744
- );
745
- }
746
- const client = frontendClient(config.sdk.url, (_a = config.sdk.options) != null ? _a : {});
747
- await client.updateSettingsFlowRaw({
821
+ await config.sdk.frontend.updateSettingsFlowRaw({
748
822
  flow: flow.id,
749
823
  updateSettingsFlowBody: body
750
824
  }).then(async (res) => {
@@ -757,8 +831,7 @@ async function onSubmitSettings({ config, flow }, {
757
831
  }
758
832
  setFlowContainer({
759
833
  flow: body2,
760
- flowType: FlowType.Settings,
761
- config
834
+ flowType: FlowType.Settings
762
835
  });
763
836
  }).catch(
764
837
  handleFlowError({
@@ -772,8 +845,7 @@ async function onSubmitSettings({ config, flow }, {
772
845
  onValidationError: (body2) => {
773
846
  setFlowContainer({
774
847
  flow: body2,
775
- flowType: FlowType.Settings,
776
- config
848
+ flowType: FlowType.Settings
777
849
  });
778
850
  },
779
851
  onRedirect
@@ -790,25 +862,18 @@ async function onSubmitSettings({ config, flow }, {
790
862
  }
791
863
  });
792
864
  }
793
- async function onSubmitVerification({ config, flow }, {
865
+ async function onSubmitVerification({ flow }, config, {
794
866
  setFlowContainer,
795
867
  body,
796
868
  onRedirect
797
869
  }) {
798
- var _a;
799
- if (!config.sdk.url) {
800
- throw new Error(
801
- `Please supply your Ory Network SDK URL to the Ory Elements configuration.`
802
- );
803
- }
804
- await frontendClient(config.sdk.url, (_a = config.sdk.options) != null ? _a : {}).updateVerificationFlowRaw({
870
+ await config.sdk.frontend.updateVerificationFlowRaw({
805
871
  flow: flow.id,
806
872
  updateVerificationFlowBody: body
807
873
  }).then(
808
874
  async (res) => setFlowContainer({
809
875
  flow: await res.value(),
810
- flowType: FlowType.Verification,
811
- config
876
+ flowType: FlowType.Verification
812
877
  })
813
878
  ).catch(
814
879
  handleFlowError({
@@ -822,8 +887,7 @@ async function onSubmitVerification({ config, flow }, {
822
887
  onValidationError: (body2) => {
823
888
  setFlowContainer({
824
889
  flow: body2,
825
- flowType: FlowType.Verification,
826
- config
890
+ flowType: FlowType.Verification
827
891
  });
828
892
  },
829
893
  onRedirect
@@ -836,6 +900,7 @@ var supportsSelectAccountPrompt = ["google", "github"];
836
900
  function useOryFormSubmit(onAfterSubmit) {
837
901
  const flowContainer = useOryFlow();
838
902
  const methods = useFormContext();
903
+ const config = useOryConfiguration();
839
904
  const handleSuccess = (flow) => {
840
905
  flowContainer.setFlowContainer(flow);
841
906
  methods.reset(computeDefaultValues(flow.flow.ui.nodes));
@@ -852,7 +917,7 @@ function useOryFormSubmit(onAfterSubmit) {
852
917
  if (submitData.method === "code" && data.code) {
853
918
  submitData.resend = "";
854
919
  }
855
- await onSubmitLogin(flowContainer, {
920
+ await onSubmitLogin(flowContainer, config, {
856
921
  onRedirect,
857
922
  setFlowContainer: handleSuccess,
858
923
  body: submitData
@@ -866,7 +931,7 @@ function useOryFormSubmit(onAfterSubmit) {
866
931
  if (submitData.method === "code" && submitData.code) {
867
932
  submitData.resend = "";
868
933
  }
869
- await onSubmitRegistration(flowContainer, {
934
+ await onSubmitRegistration(flowContainer, config, {
870
935
  onRedirect,
871
936
  setFlowContainer: handleSuccess,
872
937
  body: submitData
@@ -874,7 +939,7 @@ function useOryFormSubmit(onAfterSubmit) {
874
939
  break;
875
940
  }
876
941
  case FlowType.Verification:
877
- await onSubmitVerification(flowContainer, {
942
+ await onSubmitVerification(flowContainer, config, {
878
943
  onRedirect,
879
944
  setFlowContainer: handleSuccess,
880
945
  body: data
@@ -887,7 +952,7 @@ function useOryFormSubmit(onAfterSubmit) {
887
952
  if (data.code) {
888
953
  submitData.email = "";
889
954
  }
890
- await onSubmitRecovery(flowContainer, {
955
+ await onSubmitRecovery(flowContainer, config, {
891
956
  onRedirect,
892
957
  setFlowContainer: handleSuccess,
893
958
  body: submitData
@@ -915,7 +980,7 @@ function useOryFormSubmit(onAfterSubmit) {
915
980
  if ("passkey_remove" in submitData) {
916
981
  submitData.method = "passkey";
917
982
  }
918
- await onSubmitSettings(flowContainer, {
983
+ await onSubmitSettings(flowContainer, config, {
919
984
  onRedirect,
920
985
  setFlowContainer: handleSuccess,
921
986
  body: submitData
@@ -4304,6 +4369,6 @@ var OryLocales = {
4304
4369
  sv: sv_default
4305
4370
  };
4306
4371
 
4307
- export { HeadlessPageHeader, OryCard, OryCardContent, OryCardFooter, OryCardHeader, OryCardValidationMessages, OryConsentCard, OryForm, OryFormGroupDivider, OryFormGroups, OryFormOidcButtons, OryFormSection, OryFormSocialButtonsForm, OryLocales, OryProvider, OrySettingsCard, OryTwoStepCard, messageTestId, uiTextToFormattedMessage, useComponents, useNodeSorter, useOryFlow };
4372
+ export { HeadlessPageHeader, OryCard, OryCardContent, OryCardFooter, OryCardHeader, OryCardValidationMessages, OryConfigurationProvider, OryConsentCard, OryForm, OryFormGroupDivider, OryFormGroups, OryFormOidcButtons, OryFormSection, OryFormSocialButtonsForm, OryLocales, OryProvider, OrySettingsCard, OryTwoStepCard, messageTestId, uiTextToFormattedMessage, useComponents, useNodeSorter, useOryConfiguration, useOryFlow };
4308
4373
  //# sourceMappingURL=index.mjs.map
4309
4374
  //# sourceMappingURL=index.mjs.map