clerk-nextjs-patterns

Secure Clerk authentication patterns for Next.js middleware, Server Actions, and API routes.

Updated Mar 20, 2026
One-click install
npx skills add https://github.com/dvegaa20/alia --skill clerk-nextjs-patterns-dvegaa20
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: clerk-nextjs-patterns
Source: https://github.com/dvegaa20/alia/tree/main/.agents/skills/clerk-nextjs-patterns
Command: npx skills add https://github.com/dvegaa20/alia --skill clerk-nextjs-patterns-dvegaa20

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Prevents common authentication mistakes and security gaps when integrating Clerk with Next.js, such as missing awaits for server auth, mixing server and client APIs, incorrect HTTP status usage, and cache keys that leak user data.

Core Features & Use Cases

  • Server vs Client Auth Guidance: Clear rules for using await auth() and server imports versus client hooks to avoid undefined values and UI flashes.
  • Middleware Strategies: Patterns for public-first and protected-first middleware, route matchers, and handling pending session tasks.
  • Protecting Endpoints: Recipes for securing Server Actions, API routes with proper 401/403 semantics, and verifying tokens for external services.
  • Caching Best Practices: User- and org-scoped cache keys and revalidation strategies to prevent cross-user data leakage.
  • Use Cases: Convert client-side pages to server components, add auth to API routes, protect dashboard routes with middleware, and obtain custom JWTs for external APIs like Hasura.

Quick Start

Use the clerk-nextjs-patterns guidance to convert a client-side Next.js page that uses useAuth into a server component using await auth(), update middleware to protect /dashboard, and ensure cache keys include the userId.

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 UI flashes when converting a Next.js client page using useAuth to a server component?

To prevent UI flashes during Next.js authentication conversions, use the server-side `await auth()` API instead of client-side `useAuth` hooks. This approach ensures secure Clerk auth patterns by rendering protected content directly on the server without exposing undefined authentication states.

What is the best way to protect Next.js Server Actions and API routes with Clerk?

The best way to protect Next.js Server Actions and API routes with Clerk is to verify user sessions using awaited server APIs. Implement proper 401/403 HTTP status semantics to handle unauthorized access and enforce route authorization before executing action logic.

How do I configure Next.js middleware to protect dashboard routes using Clerk?

You can configure Next.js middleware to protect dashboard routes by defining route matchers and applying a protected-first or public-first middleware strategy. This Clerk setup ensures pending session tasks are handled correctly and unauthorized users are redirected before rendering.

Why does Next.js caching leak user data across sessions when using Clerk authentication?

Next.js caching leaks user data across sessions when cache keys lack proper user scoping. To prevent cross-user data leaks with Clerk, you must generate cache keys that include the `userId` or `orgId` and implement strict revalidation strategies tied to the authenticated session.

How do I generate a custom JWT token with Clerk for an external API like Hasura?

To generate a custom JWT token for external APIs like Hasura, use the Clerk `getToken` or `verifyToken` server APIs within your Next.js application. This provides a secure authentication pattern to authorize requests to external services.