Skip to content

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.

  1. Copy examples/with-clerk/.env.example to .env.local and set:

    • DATABASE_URL
    • CLERK_PUBLISHABLE_KEY / CLERK_SECRET_KEY (and NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY)
    • Model keys (optional; mock mode without keys)
  2. Install dependencies (already listed in the example package.json):

    Terminal window
    pnpm add @clerk/nextjs
  3. Add Clerk middleware at the project root:

middleware.ts
/* 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)(.*)",
],
};
app/api/arivie/route.ts
/* 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);
}

The canonical line uses Clerk’s server auth() helper:

lib/resolve-owner.ts
/* 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.

Terminal window
cd arivie && pnpm --filter with-clerk dev

Canonical tree: arivie/examples/with-clerk/.

Tested with @clerk/nextjs ^7.3.7 (see example package.json).