import Link from "next/link"; import { getSession, signOut } from "@/lib/auth"; import { redirect } from "next/navigation"; // `getSession()` reads cookies(), which forces dynamic rendering. // Without this, Next.js tries to prerender this page at build time // and fails with "Dynamic server usage". export const dynamic = "force-dynamic"; /** * /protected-example * * Smoke-test page that demonstrates the Neon Auth pattern. Calling * `getSession()` server-side returns the current session (null if not signed * in). The middleware in `../middleware.ts` already redirects * unauthenticated visitors to `/login`, so by the time this page renders * we always have a session. * * The page shows: * • The user's name, email, and provider * • A "Sign out" form action that calls `signOut()` from Neon Auth */ export default async function ProtectedExamplePage() { const { data: session } = await getSession(); // Defensive: middleware should have already redirected. Render a // friendly hint if we ever reach here unauthenticated. if (!session?.user) { return (

Not signed in

You should have been redirected to{" "} /login . If you can see this, the middleware matcher needs adjusting.

); } const user = session.user; return (

Protected example

You are signed in. This page is guarded by the Neon Auth middleware in middleware.ts.

Session

Name
{user.name ?? "(none)"}
Email
{user.email ?? "(none)"}
User id
{user.id ?? "(none)"}

Try it

Use the form below to sign out, or navigate to{" "} /admin {" "} (the same session is shared).

{ "use server"; await signOut(); redirect("/login"); }} className="mt-4" >
); }