Clerk

From InugamiDev/ultrathink-oss · View interactive version

Clerk is an AI agent skill from the GitHub repository InugamiDev/ultrathink-oss. > Drop-in authentication and user management — sign-in, sign-up, orgs, RBAC, webhooks.

How to use this skill

Skill file

Clerk

Drop-in authentication and user management — sign-in, sign-up, orgs, RBAC, webhooks.

When to Use

Core Patterns

Provider Setup (Next.js App Router)

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

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <ClerkProvider>
      <html><body>{children}</body></html>
    </ClerkProvider>
  );
}

Middleware (Protect Routes)

// middleware.ts
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";

const isPublic = createRouteMatcher(["/", "/sign-in(.*)", "/sign-up(.*)", "/api/webhooks(.*)"]);

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

export const config = { matcher: ["/((?!_next|.*\\.).*)"] };

Client Hooks

import { useUser, useAuth, useOrganization } from "@clerk/nextjs";

const { user, isLoaded } = useUser();           // Full user object
const { userId, getToken } = useAuth();          // Auth state + JWT access
const { organization, membership } = useOrganization(); // Active org + role

Server-Side Auth

import { auth, currentUser } from "@clerk/nextjs/server";

// Route handler or Server Component
const { userId, orgId, orgRole } = await auth();
const user = await currentUser(); // Full User object
if (!userId) redirect("/sign-in");

Webhook (Sync Users to DB)

// app/api/webhooks/clerk/route.ts
import { Webhook } from "svix";
import { WebhookEvent } from "@clerk/nextjs/server";

export async function POST(req: Request) {
  const payload = await req.text();
  const headers = Object.fromEntries(req.headers);
  const wh = new Webhook(process.env.CLERK_WEBHOOK_SECRET!);
  const event = wh.verify(payload, headers) as WebhookEvent;

  if (event.type === "user.created") await db.users.create(event.data);
  if (event.type === "user.updated") await db.users.update(event.data);
  return Response.json({ received: true });
}

Key Features

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.