Email for professionals and teams
fix(ui): sidebar crash on non-JSON responses, nav key warning, admin nav entries
Oday Bakkour682bf411be23
diff --git a/src/components/admin-nav.tsx b/src/components/admin-nav.tsxindex a3136c3..4114267 100644--- a/src/components/admin-nav.tsx+++ b/src/components/admin-nav.tsx@@ -9,6 +9,8 @@ import {Palette,BadgeDollarSign,Users,+ Route,+ Webhook,} from "lucide-react";import { cn } from "@/lib/utils";import { NavItem } from "./components-nav";@@ -27,6 +29,8 @@ const sections = [links: [{ href: "/mailboxes", label: "Mailboxes", icon: Mail },{ href: "/domains", label: "Domains", icon: Globe2 },+ { href: "/routing", label: "Routing", icon: Route },+ { href: "/webhooks", label: "Webhooks", icon: Webhook },],},{@@ -43,7 +47,6 @@ const sections = [{ href: "/branding", label: "Branding", icon: Palette },{ href: "/licenses", label: "Licenses", icon: BadgeDollarSign },// { href: "/api-keys", label: "API Keys", icon: KeyRound },- // { href: "/webhooks", label: "Webhooks", icon: Webhook }],},];@@ -64,7 +67,8 @@ export function AdminNav({ className }: { className?: string }) {if (links.length === 0) return null;return (- <section key={section.label}>+ // The first section has no label, so fall back to its first href for a stable key.+ <section key={section.label ?? links[0].href}>{!minimal && section.label && (<p className="mb-1 px-3 text-[11px] font-semibold uppercase tracking-wider text-neutral-400">{section.label}diff --git a/src/components/sidebar-state.tsx b/src/components/sidebar-state.tsxindex 80f15ed..1f7d6d4 100644--- a/src/components/sidebar-state.tsx+++ b/src/components/sidebar-state.tsx@@ -10,14 +10,26 @@ export function SidebarProvider({ children, expandedWidth = 240 }: SidebarProvidconst [storageKey, setStorageKey] = useState<string | null>(null);useEffect(() => {+ // The sidebar preference is cosmetic, so every failure here degrades to the default.+ // Guard the parse: an error response may carry an empty or non-JSON body, and an+ // unhandled rejection here surfaces as a confusing SyntaxError overlay in dev.void fetch("/api/auth/me", { cache: "no-store" })- .then((response) => response.json() as Promise<{ user?: { id?: string } }>)+ .then(async (response) => {+ if (!response.ok) return null;+ return (await response.json().catch(() => null)) as { user?: { id?: string } } | null;+ }).then((data) => {- if (!data.user?.id) return;- const key = `mailflare-sidebar-minimal:${data.user.id}`;+ const userId = data?.user?.id;+ if (!userId) return;+ const key = `mailflare-sidebar-minimal:${userId}`;setStorageKey(key);- setMinimal(localStorage.getItem(key) === "true");- });+ try {+ setMinimal(localStorage.getItem(key) === "true");+ } catch {+ // Storage can be unavailable in private windows; keep the default.+ }+ })+ .catch(() => undefined);}, []);function toggle() {diff --git a/src/lib/api/auth.ts b/src/lib/api/auth.tsindex 1a0ef9a..7d1e118 100644--- a/src/lib/api/auth.ts+++ b/src/lib/api/auth.ts@@ -1,4 +1,6 @@import { eq } from "drizzle-orm";+import { NextResponse } from "next/server";+import { getCurrentUser } from "@/lib/auth/cookies";import { getDb } from "@/db";import { apiKeys, users } from "@/db/schema";import { verifyApiKey, parseScopes } from "@/lib/api-keys";@@ -43,3 +45,16 @@ export async function authenticateApiKey(export function requireScope(scopes: string[], required: string): boolean {return scopes.includes(required) || scopes.includes("*");}++/**+ * Session auth for route handlers. `requireUser` throws a bare Error, which Next turns into+ * a 500; this returns a proper 401 response instead so unauthenticated callers get the right+ * status.+ */+export async function requireSessionUser(env: CloudflareEnv, request: Request) {+ const user = await getCurrentUser(env, request);+ if (!user) {+ return { user: null, error: NextResponse.json({ error: "Unauthorized" }, { status: 401 }) } as const;+ }+ return { user, error: null } as const;+}