Auth clerk authjs lucia

From CraftOS-dev/craftbot-agent-bundles · View interactive version

Auth clerk authjs lucia is an AI agent skill from the GitHub repository CraftOS-dev/craftbot-agent-bundles. Source: https://clerk.com/docs · https://authjs.dev/ · https://lucia-auth.com/

How to use this skill

Skill file

<!--

Source: https://clerk.com/docs · https://authjs.dev/ · https://lucia-auth.com/

Authored: June 2026 for the frontend-engineer agent bundle.

-->

Auth — Clerk, Auth.js v5, Lucia v3

Three viable auth paths in 2026: Clerk (paid SaaS, drop-in components,

fastest), Auth.js v5 (OSS, framework-agnostic, edge-ready), Lucia v3

(lightweight library + your DB). This skill picks the right one per situation

and documents the canonical setup.

When to use

"Clerk", "Auth.js", "NextAuth", "Lucia", "MFA", "passkey"

Decision tree

| Need | Pick |

|---|---|

| Fastest TTV; budget for SaaS | Clerk ($25/mo+, drop-in UI, MFA, orgs) |

| OSS, framework-agnostic, edge | Auth.js v5 |

| OSS, lightweight, fully custom | Lucia v3 |

| Enterprise SSO/SCIM | WorkOS |

| Already on Supabase | Supabase Auth |

Setup — Clerk

pnpm add @clerk/nextjs
// app/layout.tsx
import { ClerkProvider } from "@clerk/nextjs";

export default function Layout({ children }) {
  return (
    <ClerkProvider>
      <html lang="en"><body>{children}</body></html>
    </ClerkProvider>
  );
}
// middleware.ts
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";

const isProtected = createRouteMatcher(["/dashboard(.*)", "/settings(.*)"]);

export default clerkMiddleware(async (auth, req) => {
  if (isProtected(req)) await auth.protect();
});

export const config = { matcher: ["/((?!_next|.*\\..*).*)"] };
# .env.local
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_...
CLERK_SECRET_KEY=sk_test_...

Auth / API keys:

Setup — Auth.js v5

pnpm add next-auth@beta @auth/prisma-adapter   # or @auth/drizzle-adapter
pnpm dlx auth secret                            # generates AUTH_SECRET
// auth.ts
import NextAuth from "next-auth";
import GitHub from "next-auth/providers/github";
import Google from "next-auth/providers/google";
import { PrismaAdapter } from "@auth/prisma-adapter";
import { prisma } from "@/lib/prisma";

export const { handlers, auth, signIn, signOut } = NextAuth({
  adapter: PrismaAdapter(prisma),
  providers: [
    GitHub({ clientId: process.env.GITHUB_ID, clientSecret: process.env.GITHUB_SECRET }),
    Google({ clientId: process.env.GOOGLE_ID, clientSecret: process.env.GOOGLE_SECRET }),
  ],
  session: { strategy: "database" },
  callbacks: {
    async session({ session, user }) {
      session.user.id = user.id;
      return session;
    },
  },
});
// app/api/auth/[...nextauth]/route.ts
export { GET, POST } from "@/auth";
// middleware.ts
export { auth as middleware } from "@/auth";
export const config = { matcher: ["/((?!api|_next|.*\\..*).*)"] };
# .env
AUTH_SECRET=...                              # `pnpm dlx auth secret`
GITHUB_ID=...
GITHUB_SECRET=...
DATABASE_URL=postgres://...

Free / OSS — keys are from each OAuth provider.

Setup — Lucia v3

pnpm add lucia @lucia-auth/adapter-prisma oslo
// src/lib/auth.ts
import { Lucia } from "lucia";
import { PrismaAdapter } from "@lucia-auth/adapter-prisma";
import { prisma } from "./prisma";

const adapter = new PrismaAdapter(prisma.session, prisma.user);

export const lucia = new Lucia(adapter, {
  sessionCookie: {
    expires: false,
    attributes: { secure: process.env.NODE_ENV === "production" },
  },
  getUserAttributes: (data) => ({ email: data.email, name: data.name }),
});

declare module "lucia" {
  interface Register {
    Lucia: typeof lucia;
    DatabaseUserAttributes: { email: string; name: string | null };
  }
}

Free / OSS.

Common recipes — Clerk

Recipe 1 — Drop-in components

// app/sign-in/[[...sign-in]]/page.tsx
import { SignIn } from "@clerk/nextjs";

export default function SignInPage() {
  return <SignIn appearance={{ elements: { card: "shadow-xl" } }} />;
}
// Any header
import { SignedIn, SignedOut, UserButton, SignInButton } from "@clerk/nextjs";

<header>
  <SignedIn><UserButton /></SignedIn>
  <SignedOut><SignInButton mode="modal" /></SignedOut>
</header>

Recipe 2 — Server-side user

// app/dashboard/page.tsx
import { auth, currentUser } from "@clerk/nextjs/server";

export default async function Dashboard() {
  const { userId } = await auth();
  if (!userId) redirect("/sign-in");
  const user = await currentUser();
  return <h1>Hello, {user?.firstName}</h1>;
}

Recipe 3 — Org-based access control

const { has } = await auth();
if (!has({ permission: "org:billing:manage" })) {
  return new Response("Forbidden", { status: 403 });
}

Recipe 4 — Webhook to sync user → app DB

// app/api/clerk-webhook/route.ts
import { Webhook } from "svix";

export async function POST(req: Request) {
  const payload = await req.text();
  const wh = new Webhook(process.env.CLERK_WEBHOOK_SECRET!);
  let evt: any;
  try {
    evt = wh.verify(payload, Object.fromEntries(req.headers));
  } catch {
    return new Response("Bad signature", { status: 400 });
  }

  if (evt.type === "user.created") {
    await db.user.create({ data: { clerkId: evt.data.id, email: evt.data.email_addresses[0].email_address } });
  }
  return new Response("ok");
}

Common recipes — Auth.js v5

Recipe 5 — Sign-in page

"use client";
import { signIn } from "next-auth/react";

<button onClick={() => signIn("github", { callbackUrl: "/dashboard" })}>
  Sign in with GitHub
</button>

Recipe 6 — Server-side auth check

// app/dashboard/page.tsx
import { auth } from "@/auth";
import { redirect } from "next/navigation";

export default async function Dashboard() {
  const session = await auth();
  if (!session?.user) redirect("/sign-in");
  return <p>Welcome, {session.user.name}</p>;
}

Recipe 7 — Protect a route via middleware

// middleware.ts
import { auth } from "@/auth";

export default auth((req) => {
  if (!req.auth && req.nextUrl.pathname.startsWith("/dashboard")) {
    return Response.redirect(new URL("/sign-in", req.url));
  }
});

Recipe 8 — Credentials provider (email + password)

import Credentials from "next-auth/providers/credentials";

providers: [
  Credentials({
    credentials: { email: {}, password: {} },
    authorize: async (creds) => {
      const user = await db.user.findUnique({ where: { email: creds.email as string } });
      if (!user) return null;
      const valid = await bcrypt.compare(creds.password as string, user.passwordHash);
      return valid ? { id: user.id, email: user.email } : null;
    },
  }),
],
session: { strategy: "jwt" },                  // must be JWT for credentials provider

Common recipes — Lucia v3

Recipe 9 — Sign-up

// app/api/sign-up/route.ts
import { hash } from "@node-rs/argon2";
import { lucia } from "@/lib/auth";
import { generateId } from "lucia";

export async function POST(req: Request) {
  const { email, password } = await req.json();
  const passwordHash = await hash(password, { memoryCost: 19_456, timeCost: 2, outputLen: 32, parallelism: 1 });
  const userId = generateId(15);
  await db.user.create({ data: { id: userId, email, passwordHash } });
  const session = await lucia.createSession(userId, {});
  const cookie = lucia.createSessionCookie(session.id);
  return new Response(null, { headers: { "Set-Cookie": cookie.serialize() } });
}

Recipe 10 — Auth check

// src/lib/auth-helpers.ts
import { lucia } from "./auth";
import { cookies } from "next/headers";

export async function getUser() {
  const cookieStore = await cookies();
  const sessionId = cookieStore.get(lucia.sessionCookieName)?.value;
  if (!sessionId) return null;
  const { session, user } = await lucia.validateSession(sessionId);
  if (session && session.fresh) {
    const cookie = lucia.createSessionCookie(session.id);
    cookieStore.set(cookie.name, cookie.value, cookie.attributes);
  }
  return user;
}

Examples

Example 1: New Next 15 SaaS → Clerk

pnpm dlx create-next-app@latest my-saas --typescript --app --tailwind
cd my-saas
pnpm add @clerk/nextjs
# Add .env keys, wrap in ClerkProvider, add middleware.ts (Setup section)
pnpm dev
# /sign-in route works out of the box

Example 2: OSS app → Auth.js v5 + Prisma

pnpm add next-auth@beta @auth/prisma-adapter
pnpm dlx auth secret > .env  # or copy to .env.local
# Migrate schema:
pnpm dlx prisma migrate dev --name add-auth-tables
# Wire auth.ts (Setup section)
# Add GitHub OAuth app, set GITHUB_ID/SECRET
pnpm dev

Edge cases / gotchas

vars renamed (NEXTAUTH_* → AUTH_*), middleware signature changed.

CLERK_SECRET_KEY (server). Without both, requests fail silently.

credentials provider; database is preferred for OAuth-only setups.

@oslojs/crypto directly.

@auth/core adapter directly, not the Next adapter (some adapters don't

work in edge). Lucia works on edge with the right adapter.

callback/github (Auth.js) or /sso-callback` (Clerk).

subdomain sharing.

CSRF token check on mutations.

the WebAuthn adapter (beta).

Enterprise / Okta.

callbacks the OAuth provider can reach.

Sources

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.