Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Apple debug (#354)

Bill Yangb18fdb273f3e
diff --git a/client/src/api/admin/sites.ts b/client/src/api/admin/sites.ts
index 37de285ed..238c9176c 100644
--- a/client/src/api/admin/sites.ts
+++ b/client/src/api/admin/sites.ts
@@ -206,6 +206,30 @@ export function changeSiteBlockBots(siteId: number, blockBots: boolean) {
});
}
+export function useGetSiteIsPublic(siteId?: string | number) {
+ return useQuery({
+ queryKey: ["site-is-public", siteId],
+ queryFn: async () => {
+ if (!siteId) {
+ return false;
+ }
+
+ try {
+ const response = await fetch(`${BACKEND_URL}/site-is-public/${siteId}`);
+ if (response.ok) {
+ const data = await response.json();
+ return !!data.isPublic;
+ }
+ } catch (error) {
+ console.error("Error checking if site is public:", error);
+ }
+ return false;
+ },
+ staleTime: 60000, // 1 minute
+ enabled: !!siteId,
+ });
+}
+
export const useCurrentSite = () => {
const { data: activeOrganization } = authClient.useActiveOrganization();
const { data: sites } = useGetSitesFromOrg(activeOrganization?.id);
diff --git a/client/src/app/layout.tsx b/client/src/app/layout.tsx
index f92557d07..e3b16e8f4 100644
--- a/client/src/app/layout.tsx
+++ b/client/src/app/layout.tsx
@@ -1,87 +1,27 @@
"use client";
-import { BACKEND_URL } from "@/lib/const";
import QueryProvider from "@/providers/QueryProvider";
import { Inter } from "next/font/google";
-import { redirect, usePathname } from "next/navigation";
-import { useEffect, useState } from "react";
import { Toaster } from "../components/ui/sonner";
import { TooltipProvider } from "../components/ui/tooltip";
-import { userStore } from "../lib/userStore";
import { cn } from "../lib/utils";
import "./globals.css";
import Script from "next/script";
import { useStopImpersonation } from "@/hooks/useStopImpersonation";
import { ReactScan } from "./ReactScan";
import { OrganizationInitializer } from "../components/OrganizationInitializer";
+import { AuthenticationGuard } from "../components/AuthenticationGuard";
const inter = Inter({ subsets: ["latin"] });
-const publicRoutes = ["/login", "/signup", "/invitation", "/reset-password"];
-
-// Helper function to check if a site is public
-async function checkIfSiteIsPublic(siteId: string): Promise<boolean> {
- try {
- const response = await fetch(`${BACKEND_URL}/site-is-public/${siteId}`);
- if (response.ok) {
- const data = await response.json();
- return !!data.isPublic;
- }
- } catch (error) {
- console.error("Error checking if site is public:", error);
- }
- return false;
-}
-
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
- const { user, isPending } = userStore();
- const pathname = usePathname();
- const [isCheckingPublic, setIsCheckingPublic] = useState(false);
- const [isPublicSite, setIsPublicSite] = useState(false);
-
// Use the hook to expose stopImpersonating globally
useStopImpersonation();
- useEffect(() => {
- // Check if the current path could be a site path
- // Extract potential siteId from path like /{siteId} or /{siteId}/something
- const pathSegments = pathname.split("/").filter(Boolean);
- if (pathSegments.length > 0) {
- const potentialSiteId = pathSegments[0];
-
- // Don't check for public site status on obvious non-site paths
- if (!isNaN(Number(potentialSiteId))) {
- setIsCheckingPublic(true);
-
- checkIfSiteIsPublic(potentialSiteId).then((isPublic) => {
- setIsPublicSite(isPublic);
- setIsCheckingPublic(false);
- });
- }
- }
- }, [pathname]);
-
- useEffect(() => {
- // Only redirect if:
- // 1. We're not checking public status anymore
- // 2. User is not logged in
- // 3. Not on a public route
- // 4. Not on a public site
- if (
- !isPending &&
- !isCheckingPublic &&
- !user &&
- !publicRoutes.includes(pathname) &&
- !isPublicSite
- ) {
- redirect("/login");
- }
- }, [isPending, user, pathname, isCheckingPublic, isPublicSite]);
-
return (
<html lang="en" className="dark">
<ReactScan />
@@ -94,6 +34,7 @@ export default function RootLayout({
>
<QueryProvider>
<OrganizationInitializer />
+ <AuthenticationGuard />
{children}
</QueryProvider>
<Toaster />
diff --git a/client/src/components/AuthenticationGuard.tsx b/client/src/components/AuthenticationGuard.tsx
new file mode 100644
index 000000000..7b00bd920
--- /dev/null
+++ b/client/src/components/AuthenticationGuard.tsx
@@ -0,0 +1,43 @@
+"use client";
+
+import { useEffect } from "react";
+import { redirect, usePathname } from "next/navigation";
+import { userStore } from "../lib/userStore";
+import { useGetSiteIsPublic } from "../api/admin/sites";
+
+const publicRoutes = ["/login", "/signup", "/invitation", "/reset-password"];
+
+export function AuthenticationGuard() {
+ const { user, isPending } = userStore();
+ const pathname = usePathname();
+
+ // Extract potential siteId from path like /{siteId} or /{siteId}/something
+ const pathSegments = pathname.split("/").filter(Boolean);
+ const potentialSiteId =
+ pathSegments.length > 0 && !isNaN(Number(pathSegments[0]))
+ ? pathSegments[0]
+ : undefined;
+
+ // Use Tanstack Query to check if site is public
+ const { data: isPublicSite, isLoading: isCheckingPublic } =
+ useGetSiteIsPublic(potentialSiteId);
+
+ useEffect(() => {
+ // Only redirect if:
+ // 1. We're not checking public status anymore
+ // 2. User is not logged in
+ // 3. Not on a public route
+ // 4. Not on a public site
+ if (
+ !isPending &&
+ !isCheckingPublic &&
+ !user &&
+ !publicRoutes.includes(pathname) &&
+ !isPublicSite
+ ) {
+ redirect("/login");
+ }
+ }, [isPending, user, pathname, isCheckingPublic, isPublicSite]);
+
+ return null; // This component doesn't render anything
+}