Files
sofa/lib/auth/session.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

26 lines
803 B
TypeScript

import { headers } from "next/headers";
import { cache } from "react";
import { auth } from "./server";
/**
* Cached session getter — deduplicated per request via React.cache().
* Use this instead of calling auth.api.getSession() directly in server
* components, route handlers, and server actions to avoid redundant
* session lookups within a single render pass.
*/
export const getSession = cache(async () => {
return auth.api.getSession({ headers: await headers() });
});
export async function requireSession() {
const session = await getSession();
if (!session) throw new Error("Unauthorized");
return session;
}
export async function requireAdmin() {
const session = await requireSession();
if (session.user.role !== "admin") throw new Error("Forbidden");
return session;
}