import "server-only"; import { cache } from "react"; import { cookies } from "next/headers"; import { redirect } from "next/navigation"; import { sessionFromToken } from "./auth-service"; import { SESSION_COOKIE, authDestination } from "./auth-policy"; // React cache scopes this result to one render, never across users or requests. export const getSession = cache(async () => { const token = (await cookies()).get(SESSION_COOKIE)?.value; return token ? sessionFromToken(token) : null; }); export async function requireUser() { const session = await getSession(); const destination = authDestination(session); if (destination !== "/") redirect(destination); return session!; }