clerk-nextjs-patterns

Implement Clerk authentication and authorization patterns for Next.js server components, middleware, and Server Actions.

66|4|Updated Jan 6, 2026
One-click install
npx skills add https://github.com/clerk/skills --skill clerk-nextjs-patterns-clerk
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: clerk-nextjs-patterns
Source: https://github.com/clerk/skills/tree/main/skills/frameworks/clerk-nextjs-patterns
Command: npx skills add https://github.com/clerk/skills --skill clerk-nextjs-patterns-clerk

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Provides clear, secure patterns to prevent unauthorized access, session leaks, and incorrect client/server auth usage in Next.js applications using Clerk.

Core Features & Use Cases

  • Server vs Client Auth Guidance: Explains when to use await auth() and server imports versus client hooks to avoid undefined user state and flashing UI.
  • Middleware & Route Protection: Patterns for public-first and protected-first middleware strategies to centrally protect dashboards, APIs, and routes.
  • Server Actions, Token Handling & Caching: Shows how to protect Server Actions, use getToken for external APIs, manually verify JWTs, and implement user- or org-scoped caching to avoid data leakage.
  • Use Case: Convert a client-side dashboard that flashes unauthenticated content into a server component that uses auth() for seamless, secure rendering and correct caching per user.

Quick Start

Apply the clerk-nextjs-patterns guidance to replace client-side auth hooks with server-side auth(), add middleware protections for your protected routes, and ensure cache keys include userId or orgId.

Frequently Asked Questions about clerk-nextjs-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I prevent unauthorized access and session leaks in Next.js using Clerk?

Prevent unauthorized access and session leaks in Next.js by replacing client-side auth hooks with server-side auth() in Server Components. This avoids undefined user state and UI flashing while securing rendering.

Why does my Next.js dashboard flash unauthenticated content before loading?

Your Next.js dashboard flashes unauthenticated content because client-side auth hooks cause undefined user state during initial render. Converting the dashboard to a Server Component using await auth() provides seamless, secure rendering without flashing.

How do I protect Next.js Server Actions and middleware routes with Clerk?

Protect Next.js Server Actions and middleware routes by applying protected-first middleware strategies to centrally secure dashboards and APIs. This ensures Server Actions validate authentication before executing sensitive logic.

How do I implement user-scoped caching in Next.js to avoid data leakage?

Implement user-scoped caching in Next.js by including userId or orgId in cache keys. This pattern isolates cached data per user or organization, preventing data leakage across sessions.

How do I use Clerk getToken for external APIs and manually verify JWTs?

Use Clerk getToken within Next.js Server Actions or API routes to retrieve JWTs for external API authorization. Apply manual JWT verification workflows to decode and validate token signatures and claims directly.

Can I use Clerk server-side auth checks in Next.js API routes and Server Components?

Yes, Clerk supports server-side auth checks in Next.js API routes and Server Components using await auth(). This enables secure token handling and prevents unauthorized access without relying on client-side context.