Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript

Build the auth route handlers on first request

Claude6af483033c27
diff --git a/src/app/api/auth/[...all]/route.ts b/src/app/api/auth/[...all]/route.ts
index 9f04efd..7a56aa3 100644
--- a/src/app/api/auth/[...all]/route.ts
+++ b/src/app/api/auth/[...all]/route.ts
@@ -2,6 +2,34 @@ import { toNextJsHandler } from "better-auth/next-js";
import { getAuth } from "@/lib/auth/server";
-const handlers = toNextJsHandler(getAuth());
+type AuthHandlers = ReturnType<typeof toNextJsHandler>;
-export const { DELETE, GET, PATCH, POST, PUT } = handlers;
+let cachedHandlers: AuthHandlers | null = null;
+
+// Built on the first request rather than at import time, so that `next build`
+// does not need a database or auth secret to collect page data.
+function getHandlers(): AuthHandlers {
+ cachedHandlers ??= toNextJsHandler(getAuth());
+
+ return cachedHandlers;
+}
+
+export function DELETE(request: Request) {
+ return getHandlers().DELETE(request);
+}
+
+export function GET(request: Request) {
+ return getHandlers().GET(request);
+}
+
+export function PATCH(request: Request) {
+ return getHandlers().PATCH(request);
+}
+
+export function POST(request: Request) {
+ return getHandlers().POST(request);
+}
+
+export function PUT(request: Request) {
+ return getHandlers().PUT(request);
+}