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