clerk-nextjs-patterns

Protect Next.js App Router applications with Clerk middleware and auth patterns.

Updated Aug 23, 2026
One-click install
npx skills add https://github.com/awfixers-stuff/opencode-config --skill clerk-nextjs-patterns-awfixers-stuff
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: clerk-nextjs-patterns
Source: https://github.com/awfixers-stuff/opencode-config/tree/main/skills/clerk-nextjs-patterns
Command: npx skills add https://github.com/awfixers-stuff/opencode-config --skill clerk-nextjs-patterns-awfixers-stuff

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill documents vetted Clerk authentication patterns for Next.js to prevent common mistakes like flashing unauthenticated content, leaking cached data between users, and misconfigured middleware that leaves API routes unprotected.

Core Features & Use Cases

  • Middleware Strategies: public-first and protected-first approaches, route matchers, and session task handling to control access across pages and APIs.
  • Server vs Client Auth Guidance: clear rules for using await auth() and currentUser in server components versus useAuth/useUser and Show/SignedIn in client components.
  • Server Actions, API Protection & JWTs: patterns for protecting server actions, returning correct 401/403 responses, using getToken for custom JWTs to call external APIs, and manual verification when not using Clerk middleware.
  • Real-world example: migrate a client-side dashboard that uses hooks and useEffect redirects into a server component that uses await auth(), protect API routes with clerk middleware, and cache per-user data safely.

Quick Start

Convert a client-side dashboard to a server component by replacing client hooks with await auth() from @clerk/nextjs/server, add clerk middleware to protect dashboard routes, and include userId in your cache keys.

Frequently Asked Questions about clerk-nextjs-patterns

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

FAQPage Schema
How do I protect Next.js API routes with Clerk middleware?

Protect Next.js API routes by configuring Clerk middleware with public-first or protected-first strategies and route matchers to control access across pages and APIs. This ensures correct 401/403 responses and prevents misconfigured middleware from leaving endpoints unprotected.

Why does my Next.js app flash unauthenticated content before redirecting?

Flashing unauthenticated content happens when using client-side hooks like useAuth and useEffect for redirects. To fix this, convert dashboards to server components using await auth() from @clerk/nextjs/server and apply Clerk middleware to protect routes before rendering.

How do I use Clerk JWTs to call external APIs from Next.js server actions?

Use getToken from Clerk within Next.js server actions to generate custom templated JWTs for calling external APIs. This ensures secure token-based authentication when interacting with services outside your Clerk middleware-protected application boundaries.

How do I prevent caching data between different users in Next.js App Router?

Prevent leaking cached data between users by including userId or orgId in your cache keys. When using server components and await auth(), scoping cache entries to the authenticated user ensures data isolation across your Next.js application.

Do I need to manually verify Clerk JWTs when not using middleware?

Yes, you must manually verify tokens when operating Next.js routes without Clerk middleware. Manual JWT verification ensures requests remain authenticated and authorized even when standard middleware session handling is absent from your application.

What is the difference between using await auth() and useAuth in Next.js?

Use await auth() and currentUser in Next.js server components for server-side authentication, while useAuth and useUser hooks are for client components. Choosing the correct method prevents unauthenticated content flashes and ensures proper session handling.