agentleFS
Rikinshah787Rikinshah787/agent-skills-production-skills/skills/clerk/SKILL.md

clerk

Skill2 starsChanged 4 months agoMITView on GitHub

Use to add drop-in authentication (sign-in/up, sessions, orgs, user button) to a Next.js/React app and protect server code.

  • Reads credentials
  • Installs packages

5 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use clerk-custom-ui, clerk-nextjs-patterns and 4 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • elpiartheraSkill

    clerk-custom-ui

    Customize Clerk component appearance - themes, layout, colors, fonts, CSS. Use for appearance styling, visual customization, branding.

    Also in 3 projects that use this

  • elpiartheraSkill

    clerk-nextjs-patterns

    Advanced Next.js patterns - middleware, Server Actions, caching with Clerk.

    Also in 3 projects that use this

  • Harmeet10000Skill

    clerk-setup

    Add Clerk authentication to any project by following the official quickstart guides.

    Also in 3 projects that use this

  • +3 more from elpiarthera

The file

We split this skill into the rules it gives an agent

3 rules. 0 are common, 1 is distinctive, and 2 are very specific to this project.

  • 0

    Common

    Most of the 5 edited versions we read keep these.

  • 1

    Distinctive

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use when: you want auth working in minutes with prebuilt UI (<SignIn/>, <UserButton/>).

    line 21
  2. Distinctive:

    Use when: you need organizations/teams, roles, or B2B multi-tenant auth.

    line 22
  3. Very specific:

    Don't use for: cases where you must fully own the auth tables/DB (use Auth.js/Lucia or Supabase Auth).

    line 23

“Common” is measured: kept by at least 6 in 10 of the 5 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Clerk — Skillship

Drop-in authentication and user management: prebuilt UI components, hosted sign-in/up, sessions, and organizations, wired into Next.js middleware and server helpers.

🧭 When to use this skill

  • Use when: you want auth working in minutes with prebuilt UI (<SignIn/>, <UserButton/>).
  • Use when: you need organizations/teams, roles, or B2B multi-tenant auth.
  • Don't use for: cases where you must fully own the auth tables/DB (use Auth.js/Lucia or Supabase Auth).

⚡ Quickstart

1. Scaffold with the CLI (recommended — wires provider, middleware, and env keys)

npx -y clerk@latest init
# writes NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY and CLERK_SECRET_KEY to .env.local

2. Or set up manually

npm install @clerk/nextjs
# .env.local
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_...
CLERK_SECRET_KEY=sk_test_...          # SERVER ONLY — never expose in client code

3. Middleware

// Next.js 16+: name the file proxy.ts | Next.js <=15: name it middleware.ts (contents identical)
import { clerkMiddleware } from "@clerk/nextjs/server";

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)(.*)",
    "/__clerk/(.*)",
  ],
};

4. Wrap the app (ClerkProvider goes INSIDE <body>)

// app/layout.tsx
import { ClerkProvider, SignInButton, SignUpButton, Show, UserButton } from "@clerk/nextjs";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        <ClerkProvider>
          <header>
            <Show when="signed-out">
              <SignInButton />
              <SignUpButton />
            </Show>
            <Show when="signed-in">
              <UserButton />
            </Show>
          </header>
          {children}
        </ClerkProvider>
      </body>
    </html>
  );
}

🧩 Common recipes

Recipe: Read the user on the server (auth() is async — always await)

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

export default async function Page() {
  const { userId } = await auth();          // fast: just the ID / claims
  if (!userId) return <p>Sign in first.</p>;

  const user = await currentUser();          // full user object (extra fetch)
  return <p>Hello {user?.firstName}</p>;
}

Recipe: Protect a Route Handler / server action (protect close to the data)

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

export async function POST() {
  const { userId } = await auth();
  if (!userId) return new Response("Unauthorized", { status: 401 });
  // ...do the protected work
}

Recipe: Client-side user state

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

export function Profile() {
  const { isLoaded, isSignedIn, user } = useUser();
  if (!isLoaded) return null;
  return isSignedIn ? <span>{user.fullName}</span> : <span>Signed out</span>;
}

🚀 Ship to production

  • Create a production instance in the Clerk Dashboard and use its live keys as host secrets.
  • Configure your production domain + set CLERK_SECRET_KEY / NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY in the host.
  • Middleware file named correctly for your Next.js version (proxy.ts 16+, middleware.ts ≤15).
  • Enforce authorization at the data layer (route handlers/actions), not only in middleware.
  • Set CLERK_ENCRYPTION_KEY (openssl rand --hex 32) if you pass a dynamic secretKey to middleware.
  • Use @clerk/nextjs (not @clerk/clerk-react) in Next.js apps.

🔐 Security & secrets

  • CLERK_SECRET_KEY authenticates your backend to Clerk — never expose it in client code or NEXT_PUBLIC_*.
  • Protect resources as close to the data as possible; middleware alone is not a sufficient auth boundary.
  • Set authorizedParties in middleware to defend against subdomain cookie-leak attacks.

🐛 Common errors & fixes

SymptomLikely causeFix
auth() ... Promise / undefined userIdForgot to awaitconst { userId } = await auth()
Auth not applied on some routesMiddleware matcher too narrowUse the recommended matcher incl. `/(apitrpc)(.*)`
ClerkProvider errors / hydration issuesProvider wrapping <html>Put <ClerkProvider> inside <body>
Works in dev, breaks in prodStill using dev keys / domainCreate prod instance, set live keys + domain
Secret key leaked to clientUsed NEXT_PUBLIC_ for secretKeep secret server-side only

📚 Sources

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.