Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Common Clerk SDK patterns and best practices. Use when implementing authentication flows, accessing user data, or integrating Clerk SDK methods in your application. Trigger with phrases like "clerk SDK", "clerk patterns", "clerk best practices", "clerk API usage".
.claude/skills/jeremylongshore-clerk-sdk-patterns/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-02 | ✗→✓ | ▲ Improved | 2% | 0% |
| case-05 | ✗→✓ | ▲ Improved | 0% | 0% |
| case-08 | ✗→✓ | ▲ Improved | 59% | 0% |
| case-14 | ✗→✓ | ▲ Improved | 11% | 0% |
| case-10 | ✓→✗ | ▼ Worse | 38% | 0% |
Common patterns and best practices for using the Clerk SDK effectively across server components, client components, API routes, and middleware.
typescript// Server Component — use auth() for lightweight checks import { auth } from '@clerk/nextjs/server' export default async function ServerPage() { const { userId, orgId, has } = await auth() if (!userId) return <div>Not authenticated</div> if (!has({ permission: 'org:posts:create' })) return <div>No permission</div> return <div>Authorized content for {userId}</div> }
typescript// Use currentUser() when you need full user profile data import { currentUser } from '@clerk/nextjs/server' export default async function ProfilePage() { const user = await currentUser() if (!user) return null return ( <div> <h1>{user.firstName} {user.lastName}</h1> <p>{user.emailAddresses[0]?.emailAddress}</p> <img src={user.imageUrl} alt="Avatar" /> </div> ) }
typescript'use client' import { useUser, useAuth, useClerk, useSignIn } from '@clerk/nextjs' export function ClientAuthExample() { const { user, isLoaded, isSignedIn } = useUser() // Full user object const { userId, getToken, signOut } = useAuth() // Auth state + token access const { openSignIn, openUserProfile } = useClerk() // UI controls if (!isLoaded) return <div>Loading...</div> if (!isSignedIn) return <button onClick={() => openSignIn()}>Sign In</button> const fetchWithAuth = async (url: string) => { const token = await getToken() return fetch(url, { headers: { Authorization: `Bearer ${token}` }, }) } return ( <div> <p>Hello, {user.firstName}</p> <button onClick={() => openUserProfile()}>Profile</button> <button onClick={() => signOut()}>Sign Out</button> </div> ) }
typescript// middleware.ts import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server' const isPublicRoute = createRouteMatcher([ '/', '/pricing', '/sign-in(.*)', '/sign-up(.*)', '/api/webhooks(.*)', ]) export default clerkMiddleware(async (auth, req) => { if (!isPublicRoute(req)) { await auth.protect() } })
typescript// lib/db-helpers.ts import { auth } from '@clerk/nextjs/server' export async function getOrgData() { const { userId, orgId } = await auth() if (orgId) { // Org-scoped query: return data for the active organization return db.items.findMany({ where: { organizationId: orgId } }) } // Personal account: return user's own data return db.items.findMany({ where: { ownerId: userId } }) }
typescript// Generate a Supabase-compatible JWT import { auth } from '@clerk/nextjs/server' import { createClient } from '@supabase/supabase-js' export async function getSupabaseClient() { const { getToken } = await auth() const supabaseToken = await getToken({ template: 'supabase' }) return createClient( process.env.NEXT_PUBLIC_SUPABASE_URL!, process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, { global: { headers: { Authorization: `Bearer ${supabaseToken}` }, }, } ) }
Configure the JWT template in Clerk Dashboard > JWT Templates with claims:
json{ "sub": "{{user.id}}", "email": "{{user.primary_email_address}}", "role": "authenticated" }
| Error | Cause | Solution | |-------|-------|----------| | auth() returns null userId | Not in server context | Use only in Server Components or API routes | | useUser() not updating | Stale component | Check ClerkProvider wraps the component tree | | getToken() fails | JWT template not configured | Create template in Dashboard > JWT Templates | | orgId is null | No organization selected | Prompt user with <OrganizationSwitcher /> |
typescript'use server' import { auth } from '@clerk/nextjs/server' export async function createPost(title: string, content: string) { const { userId, orgId } = await auth() if (!userId) throw new Error('Unauthorized') return db.post.create({ data: { title, content, authorId: userId, orgId }, }) }
Proceed to clerk-core-workflow-a for user sign-up and sign-in flows.
| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | pass→pass | 11,873 | 5,045 | -58% | 1 | 1 | 0% | 1,708 | 2,250 | +32% | 0 | 0 | — |
case-02 | fail→pass | 12,173 | 4,840 | -60% | 1 | 1 | 0% | 2,239 | 2,278 | +2% | 0 | 0 | — |
case-03 | pass→pass | 9,953 | 4,461 | -55% | 1 | 1 | 0% | 1,822 | 2,148 | +18% | 0 | 0 | — |
case-04 | pass→pass | 6,934 | 3,539 | -49% | 1 | 1 | 0% | 1,143 | 1,950 | +71% | 0 | 0 | — |
case-05 | fail→pass | 14,044 | 6,364 | -55% | 1 | 1 | 0% | 2,564 | 2,556 | -0% | 0 | 0 | — |
case-06 | pass→pass | 13,646 | 5,471 | -60% | 1 | 1 | 0% | 2,437 | 2,198 | -10% | 0 | 0 | — |
case-07 | pass→pass | 11,982 | 6,474 | -46% | 1 | 1 | 0% | 2,333 | 2,644 | +13% | 0 | 0 | — |
case-08 | fail→pass | 23,287 | 3,222 | -86% | 1 | 1 | 0% | 1,213 | 1,928 | +59% | 0 | 0 | — |
case-09 | pass→pass | 11,633 | 5,518 | -53% | 1 | 1 | 0% | 2,192 | 2,397 | +9% | 0 | 0 | — |
case-10 | pass→fail | 12,408 | 8,547 | -31% | 1 | 1 | 0% | 2,165 | 2,997 | +38% | 0 | 0 | — |
case-11 | pass→pass | 10,643 | 5,644 | -47% | 1 | 1 | 0% | 1,895 | 2,502 | +32% | 0 | 0 | — |
case-12 | pass→pass | 6,965 | 5,224 | -25% | 1 | 1 | 0% | 1,205 | 2,277 | +89% | 0 | 0 | — |
case-13 | pass→pass | 11,458 | 4,527 | -60% | 1 | 1 | 0% | 1,830 | 2,223 | +21% | 0 | 0 | — |
case-14 | fail→pass | 13,423 | 5,937 | -56% | 1 | 1 | 0% | 2,281 | 2,521 | +11% | 0 | 0 | — |
case-15 | pass→pass | 10,064 | 3,646 | -64% | 1 | 1 | 0% | 1,798 | 1,991 | +11% | 0 | 0 | — |
case-16 | pass→pass | 5,232 | 4,257 | -19% | 1 | 1 | 0% | 995 | 2,073 | +108% | 0 | 0 | — |
case-17 | pass→pass | 11,635 | 5,233 | -55% | 1 | 1 | 0% | 2,253 | 2,437 | +8% | 0 | 0 | — |
case-18 | pass→pass | 10,611 | 7,896 | -26% | 1 | 1 | 0% | 1,900 | 2,968 | +56% | 0 | 0 | — |
case-19 | fail→fail | 10,011 | 4,606 | -54% | 1 | 1 | 0% | 1,916 | 2,055 | +7% | 0 | 0 | — |
case-20 | pass→pass | 16,143 | 11,422 | -29% | 1 | 1 | 0% | 3,403 | 3,876 | +14% | 0 | 0 | — |
case-21 | pass→pass | 19,055 | 25,363 | +33% | 1 | 1 | 0% | 4,147 | 7,045 | +70% | 0 | 0 | — |
case-22 | pass→pass | 11,385 | 9,889 | -13% | 1 | 1 | 0% | 2,380 | 3,584 | +51% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted, and 21 counted toward the lift figure. The other 1 produced results that are not comparable between the two arms, so they are excluded from the headline rather than averaged into it. The headline lift of +14 percentage points is the difference between those two pass rates over the 21 comparable cases. 1 case got worse with the skill loaded, and it is included in that figure.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.