Files
sofa/proxy.ts
T
jake 73b07f5ff6 Refactor auth session handling and server actions across app
- Overhaul `lib/auth/server.ts` and `lib/auth/session.ts`; update all API
  routes and server actions to use the revised session pattern
- Refactor server actions (settings, titles, watchlist, setup) for
  consistency with new auth layer
- Extract `SetupForm` into its own client component with `useActionState`,
  animated steps, and copyable env snippets
- Move landing page redirect logic into `app/page.tsx`; slim down
  `LandingPage` component
- Add `proxy.ts` for local dev proxying
- Minor cleanup to `NavBar`, `MobileTabBar`, and `TitleCard`
2026-03-06 17:05:49 -05:00

37 lines
1.1 KiB
TypeScript

import { getSessionCookie } from "better-auth/cookies";
import { type NextRequest, NextResponse } from "next/server";
const authRoutes = new Set(["/login", "/register", "/setup"]);
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
// this is ONLY for optimistic redirects, it does not provide any actual security
const sessionCookie = getSessionCookie(request);
// Special case for root: becomes dashboard if logged in, landing page if not
if (pathname === "/") {
return sessionCookie
? NextResponse.rewrite(new URL("/dashboard", request.url))
: NextResponse.next();
}
// Logged-in users on auth pages → dashboard
if (authRoutes.has(pathname) && sessionCookie) {
return NextResponse.redirect(new URL("/", request.url));
}
// Unauthenticated users on protected pages → login
if (!authRoutes.has(pathname) && !sessionCookie) {
return NextResponse.redirect(new URL("/login", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: [
"/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
],
};