@ory/elements-react 1.0.0-rc.2 → 1.0.0-rc.3

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
@@ -408,6 +408,110 @@ function OryFlowProvider({
408
408
  }
409
409
  );
410
410
  }
411
+
412
+ // src/client/config.ts
413
+ function isProduction() {
414
+ var _a, _b;
415
+ return ["production", "prod"].indexOf(
416
+ (_b = (_a = process.env.VERCEL_ENV) != null ? _a : process.env.NODE_ENV) != null ? _b : ""
417
+ ) > -1;
418
+ }
419
+ function frontendClient(sdkUrl, opts = {}) {
420
+ const config = new clientFetch.Configuration({
421
+ ...opts,
422
+ basePath: sdkUrl,
423
+ credentials: "include",
424
+ headers: {
425
+ Accept: "application/json",
426
+ ...opts.headers
427
+ }
428
+ });
429
+ return new clientFetch.FrontendApi(config);
430
+ }
431
+ var defaultProject = {
432
+ name: "Ory",
433
+ registration_enabled: true,
434
+ verification_enabled: true,
435
+ recovery_enabled: true,
436
+ recovery_ui_url: "/ui/recovery",
437
+ registration_ui_url: "/ui/registration",
438
+ verification_ui_url: "/ui/verification",
439
+ login_ui_url: "/ui/login",
440
+ settings_ui_url: "/ui/settings",
441
+ default_redirect_url: "/ui/welcome",
442
+ error_ui_url: "/ui/error",
443
+ default_locale: "en",
444
+ locale_behavior: "force_default"
445
+ };
446
+ function useOryConfiguration() {
447
+ const configCtx = react.useContext(OryConfigurationContext);
448
+ return {
449
+ sdk: {
450
+ ...configCtx.sdk,
451
+ frontend: frontendClient(configCtx.sdk.url, configCtx.sdk.options)
452
+ },
453
+ project: {
454
+ ...configCtx.project
455
+ }
456
+ };
457
+ }
458
+ var OryConfigurationContext = react.createContext({
459
+ sdk: computeSdkConfig({}),
460
+ project: defaultProject
461
+ });
462
+ function OryConfigurationProvider({
463
+ children,
464
+ sdk: initialConfig,
465
+ project
466
+ }) {
467
+ const configRef = react.useRef({
468
+ sdk: computeSdkConfig(initialConfig),
469
+ project: {
470
+ ...defaultProject,
471
+ ...project
472
+ }
473
+ });
474
+ return /* @__PURE__ */ jsxRuntime.jsx(OryConfigurationContext.Provider, { value: configRef.current, children });
475
+ }
476
+ function computeSdkConfig(config) {
477
+ if ((config == null ? void 0 : config.url) && typeof config.url === "string") {
478
+ console.debug("Using sdk url from config");
479
+ return {
480
+ url: config.url.replace(/\/$/, ""),
481
+ options: config.options || {}
482
+ };
483
+ }
484
+ return {
485
+ url: getSDKUrl(),
486
+ options: (config == null ? void 0 : config.options) || {}
487
+ };
488
+ }
489
+ function getSDKUrl() {
490
+ var _a;
491
+ if (typeof process !== "undefined" && process.versions && process.versions.node) {
492
+ if (isProduction()) {
493
+ const sdkUrl = (_a = process.env["NEXT_PUBLIC_ORY_SDK_URL"]) != null ? _a : process.env["ORY_SDK_URL"];
494
+ if (!sdkUrl) {
495
+ throw new Error(
496
+ "Unable to determine SDK URL. Please set NEXT_PUBLIC_ORY_SDK_URL and/or ORY_SDK_URL in production environments."
497
+ );
498
+ }
499
+ return sdkUrl.replace(/\/$/, "");
500
+ } else {
501
+ if (process.env["__NEXT_PRIVATE_ORIGIN"]) {
502
+ return process.env["__NEXT_PRIVATE_ORIGIN"].replace(/\/$/, "");
503
+ } else if (process.env["VERCEL_URL"]) {
504
+ return `https://${process.env["VERCEL_URL"]}`.replace(/\/$/, "");
505
+ }
506
+ }
507
+ }
508
+ if (typeof window !== "undefined") {
509
+ return window.location.origin;
510
+ }
511
+ throw new Error(
512
+ "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."
513
+ );
514
+ }
411
515
  function mergeTranslations(customTranslations) {
412
516
  return Object.keys(customTranslations).reduce((acc, key) => {
413
517
  acc[key] = { ...OryLocales[key], ...customTranslations[key] };
@@ -437,17 +541,18 @@ var IntlProvider = ({
437
541
  function OryProvider({
438
542
  children,
439
543
  components: Components,
544
+ config,
440
545
  ...oryFlowProps
441
546
  }) {
442
547
  var _a, _b, _c;
443
- return /* @__PURE__ */ jsxRuntime.jsx(
548
+ return /* @__PURE__ */ jsxRuntime.jsx(OryConfigurationProvider, { sdk: config.sdk, project: config.project, children: /* @__PURE__ */ jsxRuntime.jsx(
444
549
  IntlProvider,
445
550
  {
446
- locale: (_b = (_a = oryFlowProps.config.intl) == null ? void 0 : _a.locale) != null ? _b : "en",
447
- customTranslations: (_c = oryFlowProps.config.intl) == null ? void 0 : _c.customTranslations,
551
+ locale: (_b = (_a = config.intl) == null ? void 0 : _a.locale) != null ? _b : "en",
552
+ customTranslations: (_c = config.intl) == null ? void 0 : _c.customTranslations,
448
553
  children: /* @__PURE__ */ jsxRuntime.jsx(OryFlowProvider, { ...oryFlowProps, children: /* @__PURE__ */ jsxRuntime.jsx(OryComponentProvider, { components: Components, children }) })
449
554
  }
450
- );
555
+ ) });
451
556
  }
452
557
  function OryCardHeader() {
453
558
  const { Card } = useComponents();
@@ -566,18 +671,6 @@ function OryCardContent({ children }) {
566
671
  const { Card } = useComponents();
567
672
  return /* @__PURE__ */ jsxRuntime.jsx(Card.Content, { children });
568
673
  }
569
- function frontendClient(sdkUrl, opts = {}) {
570
- const config = new clientFetch.Configuration({
571
- ...opts,
572
- basePath: sdkUrl,
573
- credentials: "include",
574
- headers: {
575
- Accept: "application/json",
576
- ...opts.headers
577
- }
578
- });
579
- return new clientFetch.FrontendApi(config);
580
- }
581
674
 
582
675
  // src/util/internal.ts
583
676
  function replaceWindowFlowId(flow) {
@@ -587,7 +680,7 @@ function replaceWindowFlowId(flow) {
587
680
  }
588
681
 
589
682
  // src/util/onSubmitLogin.ts
590
- async function onSubmitLogin({ config, flow }, {
683
+ async function onSubmitLogin({ flow }, config, {
591
684
  setFlowContainer,
592
685
  body,
593
686
  onRedirect
@@ -616,7 +709,6 @@ async function onSubmitLogin({ config, flow }, {
616
709
  },
617
710
  onValidationError: (body2) => {
618
711
  setFlowContainer({
619
- config,
620
712
  flow: body2,
621
713
  flowType: clientFetch.FlowType.Login
622
714
  });
@@ -625,18 +717,12 @@ async function onSubmitLogin({ config, flow }, {
625
717
  })
626
718
  );
627
719
  }
628
- async function onSubmitRecovery({ config, flow }, {
720
+ async function onSubmitRecovery({ flow }, config, {
629
721
  setFlowContainer,
630
722
  body,
631
723
  onRedirect
632
724
  }) {
633
- var _a;
634
- if (!config.sdk.url) {
635
- throw new Error(
636
- `Please supply your Ory Network SDK url to the Ory Elements configuration.`
637
- );
638
- }
639
- await frontendClient(config.sdk.url, (_a = config.sdk.options) != null ? _a : {}).updateRecoveryFlowRaw({
725
+ await config.sdk.frontend.updateRecoveryFlowRaw({
640
726
  flow: flow.id,
641
727
  updateRecoveryFlowBody: body
642
728
  }).then(async (res) => {
@@ -649,8 +735,7 @@ async function onSubmitRecovery({ config, flow }, {
649
735
  }
650
736
  setFlowContainer({
651
737
  flow: flow2,
652
- flowType: clientFetch.FlowType.Recovery,
653
- config
738
+ flowType: clientFetch.FlowType.Recovery
654
739
  });
655
740
  }).catch(
656
741
  clientFetch.handleFlowError({
@@ -668,8 +753,7 @@ async function onSubmitRecovery({ config, flow }, {
668
753
  } else {
669
754
  setFlowContainer({
670
755
  flow: body2,
671
- flowType: clientFetch.FlowType.Recovery,
672
- config
756
+ flowType: clientFetch.FlowType.Recovery
673
757
  });
674
758
  }
675
759
  },
@@ -690,19 +774,12 @@ function handleContinueWithRecoveryUIError(error, config, onRedirect) {
690
774
  }
691
775
  onRedirect(clientFetch.recoveryUrl(config), true);
692
776
  }
693
- async function onSubmitRegistration({ config, flow }, {
777
+ async function onSubmitRegistration({ flow }, config, {
694
778
  setFlowContainer,
695
779
  body,
696
780
  onRedirect
697
781
  }) {
698
- var _a;
699
- if (!config.sdk.url) {
700
- throw new Error(
701
- `Please supply your Ory Network SDK url to the Ory Elements configuration.`
702
- );
703
- }
704
- const client = frontendClient(config.sdk.url, (_a = config.sdk.options) != null ? _a : {});
705
- await client.updateRegistrationFlowRaw({
782
+ await config.sdk.frontend.updateRegistrationFlowRaw({
706
783
  flow: flow.id,
707
784
  updateRegistrationFlowBody: body
708
785
  }).then(async (res) => {
@@ -726,27 +803,19 @@ async function onSubmitRegistration({ config, flow }, {
726
803
  onValidationError: (body2) => {
727
804
  setFlowContainer({
728
805
  flow: body2,
729
- flowType: clientFetch.FlowType.Registration,
730
- config
806
+ flowType: clientFetch.FlowType.Registration
731
807
  });
732
808
  },
733
809
  onRedirect
734
810
  })
735
811
  );
736
812
  }
737
- async function onSubmitSettings({ config, flow }, {
813
+ async function onSubmitSettings({ flow }, config, {
738
814
  setFlowContainer,
739
815
  body,
740
816
  onRedirect
741
817
  }) {
742
- var _a;
743
- if (!config.sdk.url) {
744
- throw new Error(
745
- `Please supply your Ory Network SDK url to the Ory Elements configuration.`
746
- );
747
- }
748
- const client = frontendClient(config.sdk.url, (_a = config.sdk.options) != null ? _a : {});
749
- await client.updateSettingsFlowRaw({
818
+ await config.sdk.frontend.updateSettingsFlowRaw({
750
819
  flow: flow.id,
751
820
  updateSettingsFlowBody: body
752
821
  }).then(async (res) => {
@@ -759,8 +828,7 @@ async function onSubmitSettings({ config, flow }, {
759
828
  }
760
829
  setFlowContainer({
761
830
  flow: body2,
762
- flowType: clientFetch.FlowType.Settings,
763
- config
831
+ flowType: clientFetch.FlowType.Settings
764
832
  });
765
833
  }).catch(
766
834
  clientFetch.handleFlowError({
@@ -774,8 +842,7 @@ async function onSubmitSettings({ config, flow }, {
774
842
  onValidationError: (body2) => {
775
843
  setFlowContainer({
776
844
  flow: body2,
777
- flowType: clientFetch.FlowType.Settings,
778
- config
845
+ flowType: clientFetch.FlowType.Settings
779
846
  });
780
847
  },
781
848
  onRedirect
@@ -792,25 +859,18 @@ async function onSubmitSettings({ config, flow }, {
792
859
  }
793
860
  });
794
861
  }
795
- async function onSubmitVerification({ config, flow }, {
862
+ async function onSubmitVerification({ flow }, config, {
796
863
  setFlowContainer,
797
864
  body,
798
865
  onRedirect
799
866
  }) {
800
- var _a;
801
- if (!config.sdk.url) {
802
- throw new Error(
803
- `Please supply your Ory Network SDK URL to the Ory Elements configuration.`
804
- );
805
- }
806
- await frontendClient(config.sdk.url, (_a = config.sdk.options) != null ? _a : {}).updateVerificationFlowRaw({
867
+ await config.sdk.frontend.updateVerificationFlowRaw({
807
868
  flow: flow.id,
808
869
  updateVerificationFlowBody: body
809
870
  }).then(
810
871
  async (res) => setFlowContainer({
811
872
  flow: await res.value(),
812
- flowType: clientFetch.FlowType.Verification,
813
- config
873
+ flowType: clientFetch.FlowType.Verification
814
874
  })
815
875
  ).catch(
816
876
  clientFetch.handleFlowError({
@@ -824,8 +884,7 @@ async function onSubmitVerification({ config, flow }, {
824
884
  onValidationError: (body2) => {
825
885
  setFlowContainer({
826
886
  flow: body2,
827
- flowType: clientFetch.FlowType.Verification,
828
- config
887
+ flowType: clientFetch.FlowType.Verification
829
888
  });
830
889
  },
831
890
  onRedirect
@@ -838,6 +897,7 @@ var supportsSelectAccountPrompt = ["google", "github"];
838
897
  function useOryFormSubmit(onAfterSubmit) {
839
898
  const flowContainer = useOryFlow();
840
899
  const methods = reactHookForm.useFormContext();
900
+ const config = useOryConfiguration();
841
901
  const handleSuccess = (flow) => {
842
902
  flowContainer.setFlowContainer(flow);
843
903
  methods.reset(computeDefaultValues(flow.flow.ui.nodes));
@@ -854,7 +914,7 @@ function useOryFormSubmit(onAfterSubmit) {
854
914
  if (submitData.method === "code" && data.code) {
855
915
  submitData.resend = "";
856
916
  }
857
- await onSubmitLogin(flowContainer, {
917
+ await onSubmitLogin(flowContainer, config, {
858
918
  onRedirect,
859
919
  setFlowContainer: handleSuccess,
860
920
  body: submitData
@@ -868,7 +928,7 @@ function useOryFormSubmit(onAfterSubmit) {
868
928
  if (submitData.method === "code" && submitData.code) {
869
929
  submitData.resend = "";
870
930
  }
871
- await onSubmitRegistration(flowContainer, {
931
+ await onSubmitRegistration(flowContainer, config, {
872
932
  onRedirect,
873
933
  setFlowContainer: handleSuccess,
874
934
  body: submitData
@@ -876,7 +936,7 @@ function useOryFormSubmit(onAfterSubmit) {
876
936
  break;
877
937
  }
878
938
  case clientFetch.FlowType.Verification:
879
- await onSubmitVerification(flowContainer, {
939
+ await onSubmitVerification(flowContainer, config, {
880
940
  onRedirect,
881
941
  setFlowContainer: handleSuccess,
882
942
  body: data
@@ -889,7 +949,7 @@ function useOryFormSubmit(onAfterSubmit) {
889
949
  if (data.code) {
890
950
  submitData.email = "";
891
951
  }
892
- await onSubmitRecovery(flowContainer, {
952
+ await onSubmitRecovery(flowContainer, config, {
893
953
  onRedirect,
894
954
  setFlowContainer: handleSuccess,
895
955
  body: submitData
@@ -917,7 +977,7 @@ function useOryFormSubmit(onAfterSubmit) {
917
977
  if ("passkey_remove" in submitData) {
918
978
  submitData.method = "passkey";
919
979
  }
920
- await onSubmitSettings(flowContainer, {
980
+ await onSubmitSettings(flowContainer, config, {
921
981
  onRedirect,
922
982
  setFlowContainer: handleSuccess,
923
983
  body: submitData
@@ -1237,7 +1297,7 @@ function OryTwoStepCardStateMethodActive({
1237
1297
  onAfterSubmit: handleAfterFormSubmit(dispatchFormState),
1238
1298
  children: /* @__PURE__ */ jsxRuntime.jsxs(Form.Group, { children: [
1239
1299
  ui.nodes.filter(
1240
- (n) => clientFetch.isUiNodeScriptAttributes(n.attributes) || n.group === clientFetch.UiNodeGroupEnum.Captcha || n.group === clientFetch.UiNodeGroupEnum.Default || n.group === clientFetch.UiNodeGroupEnum.Profile
1300
+ (n) => clientFetch.isUiNodeScriptAttributes(n.attributes) || n.group === clientFetch.UiNodeGroupEnum.Default || n.group === clientFetch.UiNodeGroupEnum.Profile
1241
1301
  ).map((node, k) => /* @__PURE__ */ jsxRuntime.jsx(Node, { node }, k)),
1242
1302
  finalNodes.sort(sortNodes).map((node, k) => /* @__PURE__ */ jsxRuntime.jsx(Node, { node }, k))
1243
1303
  ] })
@@ -4312,6 +4372,7 @@ exports.OryCardContent = OryCardContent;
4312
4372
  exports.OryCardFooter = OryCardFooter;
4313
4373
  exports.OryCardHeader = OryCardHeader;
4314
4374
  exports.OryCardValidationMessages = OryCardValidationMessages;
4375
+ exports.OryConfigurationProvider = OryConfigurationProvider;
4315
4376
  exports.OryConsentCard = OryConsentCard;
4316
4377
  exports.OryForm = OryForm;
4317
4378
  exports.OryFormGroupDivider = OryFormGroupDivider;
@@ -4327,6 +4388,7 @@ exports.messageTestId = messageTestId;
4327
4388
  exports.uiTextToFormattedMessage = uiTextToFormattedMessage;
4328
4389
  exports.useComponents = useComponents;
4329
4390
  exports.useNodeSorter = useNodeSorter;
4391
+ exports.useOryConfiguration = useOryConfiguration;
4330
4392
  exports.useOryFlow = useOryFlow;
4331
4393
  //# sourceMappingURL=index.js.map
4332
4394
  //# sourceMappingURL=index.js.map