Clerk
Use Clerk when your app already authenticates users with @clerk/nextjs and you want ownerId to follow Clerk’s userId without building custom JWT plumbing.
-
Copy
examples/with-clerk/.env.exampleto.env.localand set:DATABASE_URLCLERK_PUBLISHABLE_KEY/CLERK_SECRET_KEY(andNEXT_PUBLIC_CLERK_PUBLISHABLE_KEY)- Model keys (optional; mock mode without keys)
-
Install dependencies (already listed in the example
package.json):Terminal window pnpm add @clerk/nextjs -
Add Clerk middleware at the project root:
/* SPDX-License-Identifier: Apache-2.0 */import { clerkMiddleware } from "@clerk/nextjs/server";import { assertAuthBypassAllowed } from "./lib/auth-bypass";
assertAuthBypassAllowed();
export default clerkMiddleware();
export const config = { matcher: [ "/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)", "/(api|trpc)(.*)", ],};Route handler
Section titled “Route handler”/* SPDX-License-Identifier: Apache-2.0 */import { getArivieRuntimeForOwner } from "../../../arivie.config";import { resolveOwnerId } from "../../../lib/resolve-owner";
export async function POST(req: Request): Promise<Response> { const ownerId = await resolveOwnerId(req); const { arivie } = await getArivieRuntimeForOwner(ownerId); return arivie.next.POST(req);}ownerId derivation
Section titled “ownerId derivation”The canonical line uses Clerk’s server auth() helper:
/* SPDX-License-Identifier: Apache-2.0 */import { auth } from "@clerk/nextjs/server";import { BYPASS_OWNER_ID, isAuthBypassRequest } from "./auth-bypass";
export async function resolveOwnerId(req: Request): Promise<string> { if (isAuthBypassRequest(req)) { return BYPASS_OWNER_ID; } const session = await auth(); if (session.userId != null && session.userId.length > 0) { return session.userId; } return process.env.ARIVIE_OWNER_ID ?? "with-clerk-owner";}When session.userId is present it becomes ownerId. CI sets ARIVIE_AUTH_BYPASS=1 to skip Clerk and use a fixed bypass owner.
Runnable example
Section titled “Runnable example”cd arivie && pnpm --filter with-clerk devCanonical tree: arivie/examples/with-clerk/.
Tested with @clerk/nextjs ^7.3.7 (see example package.json).