How the pieces connect
Clerk runs as a hosted identity service; your Next.js app holds no password database. The flow has three runtime surfaces. First, <ClerkProvider> in the root layout hydrates the client with the active session and exposes hooks like useUser() and useAuth(). Second, clerkMiddleware() in middleware.ts reads the __session cookie on every matched request at the edge, before a route renders, and can short-circuit unauthenticated traffic. Third, Server Components and Route Handlers call auth() from @clerk/nextjs/server, which returns userId, sessionId, and orgId from the verified JWT — no client round-trip and no extra fetch to Clerk's API for the common case. The session token is a short-lived JWT signed by Clerk; the middleware refreshes it transparently. Because auth() reads the request context, it only works inside the App Router request lifecycle, not in arbitrary module scope.