@insforge/react 0.5.4 → 0.5.6

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.d.cts CHANGED
@@ -10,6 +10,7 @@ import { CreateSessionResponse, CreateUserResponse, ResetPasswordResponse, GetPu
10
10
  export { useAuth, usePublicAuthConfig, useUser } from './hooks.cjs';
11
11
  export { checkPasswordStrength, createPasswordSchema, emailSchema, passwordSchema, resolveAuthPath, resolveAuthUrl, validateEmail, validatePassword } from './lib.cjs';
12
12
  export { BrowserNavigationAdapter, NavigationAdapter, NavigationProvider, NavigationProviderProps, useNavigationAdapter, useSearchParams } from './navigation.cjs';
13
+ export { RouteGuard, RouteGuardProps } from './routes.cjs';
13
14
  import 'zod';
14
15
 
15
16
  interface InsforgeContextValue {
@@ -63,6 +64,7 @@ interface InsforgeContextValue {
63
64
  loginWithOAuth: (provider: OAuthProvider, redirectTo: string) => Promise<void>;
64
65
  getPublicAuthConfig: () => Promise<GetPublicAuthConfigResponse | null>;
65
66
  baseUrl: string;
67
+ afterSignInUrl: string;
66
68
  }
67
69
  interface InsforgeProviderProps {
68
70
  children: ReactNode;
@@ -90,7 +92,7 @@ interface InsforgeProviderProps {
90
92
  * export default function App() {
91
93
  * return (
92
94
  * <InsforgeProvider
93
- * baseUrl={process.env.VITE_INSFORGE_BASE_URL}
95
+ * baseUrl={import.meta.env.VITE_INSFORGE_BASE_URL}
94
96
  * afterSignInUrl="/dashboard"
95
97
  * >
96
98
  * {children}
package/dist/index.d.ts CHANGED
@@ -10,6 +10,7 @@ import { CreateSessionResponse, CreateUserResponse, ResetPasswordResponse, GetPu
10
10
  export { useAuth, usePublicAuthConfig, useUser } from './hooks.js';
11
11
  export { checkPasswordStrength, createPasswordSchema, emailSchema, passwordSchema, resolveAuthPath, resolveAuthUrl, validateEmail, validatePassword } from './lib.js';
12
12
  export { BrowserNavigationAdapter, NavigationAdapter, NavigationProvider, NavigationProviderProps, useNavigationAdapter, useSearchParams } from './navigation.js';
13
+ export { RouteGuard, RouteGuardProps } from './routes.js';
13
14
  import 'zod';
14
15
 
15
16
  interface InsforgeContextValue {
@@ -63,6 +64,7 @@ interface InsforgeContextValue {
63
64
  loginWithOAuth: (provider: OAuthProvider, redirectTo: string) => Promise<void>;
64
65
  getPublicAuthConfig: () => Promise<GetPublicAuthConfigResponse | null>;
65
66
  baseUrl: string;
67
+ afterSignInUrl: string;
66
68
  }
67
69
  interface InsforgeProviderProps {
68
70
  children: ReactNode;
@@ -90,7 +92,7 @@ interface InsforgeProviderProps {
90
92
  * export default function App() {
91
93
  * return (
92
94
  * <InsforgeProvider
93
- * baseUrl={process.env.VITE_INSFORGE_BASE_URL}
95
+ * baseUrl={import.meta.env.VITE_INSFORGE_BASE_URL}
94
96
  * afterSignInUrl="/dashboard"
95
97
  * >
96
98
  * {children}
package/dist/index.js CHANGED
@@ -26,7 +26,10 @@ function NavigationProvider({ adapter, children }) {
26
26
  function useNavigationAdapter() {
27
27
  const adapter = useContext(NavigationContext);
28
28
  if (!adapter) {
29
- throw new Error("useNavigationAdapter must be used within NavigationProvider");
29
+ return {
30
+ useSearchParams: () => new URLSearchParams(),
31
+ Link: ({ href, children }) => /* @__PURE__ */ jsx("a", { href, children })
32
+ };
30
33
  }
31
34
  return adapter;
32
35
  }
@@ -402,14 +405,15 @@ function InsforgeProvider({
402
405
  signOut,
403
406
  updateUser,
404
407
  reloadAuth: loadAuthState,
405
- baseUrl,
406
408
  sendVerificationEmail,
407
409
  sendResetPasswordEmail,
408
410
  resetPassword,
409
411
  verifyEmail,
410
412
  exchangeResetPasswordToken,
411
413
  getPublicAuthConfig,
412
- loginWithOAuth
414
+ loginWithOAuth,
415
+ baseUrl,
416
+ afterSignInUrl
413
417
  },
414
418
  children
415
419
  }
@@ -438,7 +442,8 @@ function useInsforge() {
438
442
  loginWithOAuth: async () => {
439
443
  },
440
444
  getPublicAuthConfig: async () => null,
441
- baseUrl: ""
445
+ baseUrl: "",
446
+ afterSignInUrl: "/"
442
447
  };
443
448
  }
444
449
  return context;
@@ -2464,7 +2469,97 @@ function useUser() {
2464
2469
  const { user, isLoaded, updateUser, setUser } = useInsforge();
2465
2470
  return { user, isLoaded, updateUser, setUser };
2466
2471
  }
2472
+ function RouteGuard({
2473
+ children,
2474
+ builtInAuth = true,
2475
+ paths = {},
2476
+ publicRoutes = [],
2477
+ loadingFallback
2478
+ }) {
2479
+ const { isSignedIn, isLoaded, afterSignInUrl, baseUrl } = useInsforge();
2480
+ const { signIn = "/sign-in", signUp = "/sign-up", forgotPassword = "/forgot-password" } = paths;
2481
+ const [currentPath, setCurrentPath] = useState("");
2482
+ useEffect(() => {
2483
+ const updatePath = () => {
2484
+ const path = window.location.hash ? window.location.hash.slice(1) : window.location.pathname;
2485
+ setCurrentPath(path || "/");
2486
+ };
2487
+ updatePath();
2488
+ window.addEventListener("hashchange", updatePath);
2489
+ window.addEventListener("popstate", updatePath);
2490
+ return () => {
2491
+ window.removeEventListener("hashchange", updatePath);
2492
+ window.removeEventListener("popstate", updatePath);
2493
+ };
2494
+ }, []);
2495
+ const isPublicRoute = publicRoutes.some((route) => {
2496
+ if (route.endsWith("/*")) {
2497
+ const prefix = route.slice(0, -2);
2498
+ return currentPath.startsWith(prefix);
2499
+ }
2500
+ return currentPath === route || currentPath.startsWith(route + "?");
2501
+ });
2502
+ useEffect(() => {
2503
+ if (!isLoaded) return;
2504
+ if (isSignedIn) return;
2505
+ if (!isSignedIn) {
2506
+ if (builtInAuth) {
2507
+ const isSignInPage = currentPath === signIn || currentPath.startsWith(signIn + "?");
2508
+ const isSignUpPage = currentPath === signUp || currentPath.startsWith(signUp + "?");
2509
+ const isForgotPasswordPage = currentPath === forgotPassword || currentPath.startsWith(forgotPassword + "?");
2510
+ if (isSignInPage) {
2511
+ const redirectUrl = new URL(afterSignInUrl, window.location.origin).href;
2512
+ const authUrl = new URL("/auth/sign-in", baseUrl);
2513
+ authUrl.searchParams.set("redirect", redirectUrl);
2514
+ window.location.replace(authUrl.toString());
2515
+ return;
2516
+ }
2517
+ if (isSignUpPage) {
2518
+ const redirectUrl = new URL(afterSignInUrl, window.location.origin).href;
2519
+ const authUrl = new URL("/auth/sign-up", baseUrl);
2520
+ authUrl.searchParams.set("redirect", redirectUrl);
2521
+ window.location.replace(authUrl.toString());
2522
+ return;
2523
+ }
2524
+ if (isForgotPasswordPage) {
2525
+ const authUrl = new URL("/auth/forgot-password", baseUrl);
2526
+ window.location.replace(authUrl.toString());
2527
+ return;
2528
+ }
2529
+ if (isPublicRoute) {
2530
+ return;
2531
+ } else {
2532
+ const redirectUrl = new URL(afterSignInUrl, window.location.origin).href;
2533
+ const authUrl = new URL("/auth/sign-in", baseUrl);
2534
+ authUrl.searchParams.set("redirect", redirectUrl);
2535
+ window.location.replace(authUrl.toString());
2536
+ }
2537
+ } else {
2538
+ if (isPublicRoute) {
2539
+ return;
2540
+ } else {
2541
+ window.location.href = signIn + "?redirect=" + afterSignInUrl;
2542
+ }
2543
+ }
2544
+ }
2545
+ }, [
2546
+ isLoaded,
2547
+ isSignedIn,
2548
+ currentPath,
2549
+ isPublicRoute,
2550
+ builtInAuth,
2551
+ baseUrl,
2552
+ signIn,
2553
+ signUp,
2554
+ forgotPassword,
2555
+ afterSignInUrl
2556
+ ]);
2557
+ if (!isLoaded) {
2558
+ return /* @__PURE__ */ jsx(Fragment, { children: loadingFallback });
2559
+ }
2560
+ return /* @__PURE__ */ jsx(Fragment, { children });
2561
+ }
2467
2562
 
2468
- export { AuthBranding, AuthContainer, AuthDivider, AuthEmailVerificationStep, AuthErrorBanner, AuthFormField, AuthHeader, AuthLink, AuthOAuthButton, AuthOAuthProviders, AuthPasswordField, AuthPasswordStrengthIndicator, AuthResetPasswordVerificationStep, AuthSubmitButton, AuthVerificationCodeInput, BrowserNavigationAdapter, ForgotPassword, ForgotPasswordForm, InsforgeProvider, NavigationProvider, OAUTH_PROVIDER_CONFIG, Protect, ResetPassword, ResetPasswordForm, SignIn, SignInForm, SignUp, SignUpForm, SignedIn, SignedOut, UserButton, VerifyEmail, VerifyEmailStatus, checkPasswordStrength, createPasswordSchema, emailSchema, getAllProviderConfigs, getProviderConfig, passwordSchema, resolveAuthPath, resolveAuthUrl, useAuth, useInsforge, useNavigationAdapter, usePublicAuthConfig, useSearchParams, useUser, validateEmail, validatePassword };
2563
+ export { AuthBranding, AuthContainer, AuthDivider, AuthEmailVerificationStep, AuthErrorBanner, AuthFormField, AuthHeader, AuthLink, AuthOAuthButton, AuthOAuthProviders, AuthPasswordField, AuthPasswordStrengthIndicator, AuthResetPasswordVerificationStep, AuthSubmitButton, AuthVerificationCodeInput, BrowserNavigationAdapter, ForgotPassword, ForgotPasswordForm, InsforgeProvider, NavigationProvider, OAUTH_PROVIDER_CONFIG, Protect, ResetPassword, ResetPasswordForm, RouteGuard, SignIn, SignInForm, SignUp, SignUpForm, SignedIn, SignedOut, UserButton, VerifyEmail, VerifyEmailStatus, checkPasswordStrength, createPasswordSchema, emailSchema, getAllProviderConfigs, getProviderConfig, passwordSchema, resolveAuthPath, resolveAuthUrl, useAuth, useInsforge, useNavigationAdapter, usePublicAuthConfig, useSearchParams, useUser, validateEmail, validatePassword };
2469
2564
  //# sourceMappingURL=index.js.map
2470
2565
  //# sourceMappingURL=index.js.map