@insforge/react 0.4.5 → 0.4.7

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.
@@ -199,6 +199,28 @@ function createRequirements(config) {
199
199
  }
200
200
  return requirements;
201
201
  }
202
+
203
+ // src/lib/path-utils.ts
204
+ function resolveAuthPath(targetPath) {
205
+ if (typeof window === "undefined") {
206
+ return targetPath;
207
+ }
208
+ const currentPath = window.location.pathname;
209
+ if (currentPath.startsWith("/auth/")) {
210
+ if (targetPath.startsWith("/auth/")) {
211
+ return targetPath;
212
+ }
213
+ return `/auth${targetPath}`;
214
+ }
215
+ return targetPath;
216
+ }
217
+ function resolveAuthUrl(targetPath, searchParams) {
218
+ const resolvedPath = resolveAuthPath(targetPath);
219
+ if (!searchParams || searchParams.toString() === "") {
220
+ return resolvedPath;
221
+ }
222
+ return `${resolvedPath}?${searchParams.toString()}`;
223
+ }
202
224
  function AuthPasswordField({
203
225
  label,
204
226
  id,
@@ -211,6 +233,10 @@ function AuthPasswordField({
211
233
  }) {
212
234
  const [showPassword, setShowPassword] = react.useState(false);
213
235
  const [showStrength, setShowStrength] = react.useState(false);
236
+ const resolvedForgotPasswordHref = react.useMemo(
237
+ () => forgotPasswordLink ? resolveAuthPath(forgotPasswordLink.href) : void 0,
238
+ [forgotPasswordLink]
239
+ );
214
240
  const handleFocus = (e) => {
215
241
  if (showStrengthIndicator) {
216
242
  setShowStrength(true);
@@ -220,7 +246,7 @@ function AuthPasswordField({
220
246
  return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "if-passwordField if-internal-p5w9m7", children: [
221
247
  (label || forgotPasswordLink) && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "if-passwordField-labelRow", children: [
222
248
  /* @__PURE__ */ jsxRuntime.jsx("label", { htmlFor: id, className: "if-passwordField-label", children: label }),
223
- forgotPasswordLink && /* @__PURE__ */ jsxRuntime.jsx("a", { href: forgotPasswordLink.href, className: "if-passwordField-forgotLink", children: forgotPasswordLink.text || "Forget Password?" })
249
+ forgotPasswordLink && resolvedForgotPasswordHref && /* @__PURE__ */ jsxRuntime.jsx("a", { href: resolvedForgotPasswordHref, className: "if-passwordField-forgotLink", children: forgotPasswordLink.text || "Forget Password?" })
224
250
  ] }),
225
251
  /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "if-passwordField-inputWrapper", children: [
226
252
  /* @__PURE__ */ jsxRuntime.jsx(
@@ -273,19 +299,7 @@ function AuthSubmitButton({
273
299
  }
274
300
  function AuthLink({ text, linkText, href }) {
275
301
  const [searchParams] = reactRouterDom.useSearchParams();
276
- const currentSearch = searchParams.toString();
277
- const finalHref = (() => {
278
- if (!currentSearch) {
279
- return href;
280
- }
281
- try {
282
- const url = new URL(href, window.location.origin);
283
- url.search = currentSearch;
284
- return url.pathname + url.search;
285
- } catch {
286
- return href;
287
- }
288
- })();
302
+ const finalHref = resolveAuthUrl(href, searchParams);
289
303
  return /* @__PURE__ */ jsxRuntime.jsxs("p", { className: "if-authLink if-internal-al5w9p", children: [
290
304
  text && /* @__PURE__ */ jsxRuntime.jsx("span", { className: "if-authLink-text", children: text }),
291
305
  text && " ",
@@ -1495,12 +1509,9 @@ function ForgotPassword({ onError, ...uiProps }) {
1495
1509
  const result = await resetPassword(resetToken, newPassword);
1496
1510
  if (result?.message) {
1497
1511
  setSuccess(true);
1498
- const signInUrl = new URL("/sign-in", window.location.origin);
1499
- searchParams.forEach((value, key) => {
1500
- signInUrl.searchParams.set(key, value);
1501
- });
1512
+ const signInUrl = resolveAuthUrl("/sign-in", searchParams);
1502
1513
  setTimeout(() => {
1503
- window.location.href = signInUrl.toString();
1514
+ window.location.href = signInUrl;
1504
1515
  }, 2e3);
1505
1516
  } else {
1506
1517
  const errorMessage = "Failed to reset password";
@@ -1918,23 +1929,24 @@ function Protect({
1918
1929
  onRedirect
1919
1930
  }) {
1920
1931
  const { isSignedIn, isLoaded, user } = useInsforge();
1932
+ const resolvedRedirectTo = react.useMemo(() => resolveAuthPath(redirectTo), [redirectTo]);
1921
1933
  react.useEffect(() => {
1922
1934
  if (isLoaded && !isSignedIn) {
1923
1935
  if (onRedirect) {
1924
- onRedirect(redirectTo);
1936
+ onRedirect(resolvedRedirectTo);
1925
1937
  } else {
1926
- window.location.href = redirectTo;
1938
+ window.location.href = resolvedRedirectTo;
1927
1939
  }
1928
1940
  } else if (isLoaded && isSignedIn && condition && user) {
1929
1941
  if (!condition(user)) {
1930
1942
  if (onRedirect) {
1931
- onRedirect(redirectTo);
1943
+ onRedirect(resolvedRedirectTo);
1932
1944
  } else {
1933
- window.location.href = redirectTo;
1945
+ window.location.href = resolvedRedirectTo;
1934
1946
  }
1935
1947
  }
1936
1948
  }
1937
- }, [isLoaded, isSignedIn, redirectTo, condition, user, onRedirect]);
1949
+ }, [isLoaded, isSignedIn, resolvedRedirectTo, condition, user, onRedirect]);
1938
1950
  if (!isLoaded) {
1939
1951
  return fallback || /* @__PURE__ */ jsxRuntime.jsx("div", { className: "insforge-loading", children: "Loading..." });
1940
1952
  }