/clerk:nextjs-integrationfrom clerk
Integrates Clerk authentication into Next.js applications with App Router and Pages Router support. Covers installation, middleware, protected routes, and server/client component auth.
Ships in clerk. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them; scripts run unread.
This skill provides complete Clerk authentication integration for Next.js applications, supporting both App Router (Next.js 13+) and Pages Router patterns. It covers installation, middleware configuration, authentication helpers, and protected route patterns.
Install Clerk SDK and configure environment variables:
# Run automated installation script
bash ./skills/nextjs-integration/scripts/install-clerk.sh
# Or manually install
npm install @clerk/nextjs
Environment Variables:
# Create .env.local with Clerk credentials
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_your_key_here
CLERK_SECRET_KEY=sk_test_your_key_here
# Optional: Customize sign-in/sign-up URLs
NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in
NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up
NEXT_PUBLIC_CLERK_AFTER_SIGN_IN_URL=/dashboard
NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL=/onboarding
What This Does:
Configure Clerk for Next.js App Router (13.4+):
# Run App Router setup script
bash ./skills/nextjs-integration/scripts/setup-app-router.sh
Files Created:
middleware.ts - Route protection at edgeapp/layout.tsx - ClerkProvider wrapperapp/sign-in/[[...sign-in]]/page.tsx - Sign-in pageapp/sign-up/[[...sign-up]]/page.tsx - Sign-up pageApp Router Features:
auth() helperuseAuth() hookCopy Template Files:
# Middleware configuration
cp ./skills/nextjs-integration/templates/app-router/middleware.ts ./middleware.ts
# Root layout with ClerkProvider
cp ./skills/nextjs-integration/templates/app-router/layout.tsx ./app/layout.tsx
Configure Clerk for Next.js Pages Router (12.x and earlier):
# Run Pages Router setup script
bash ./skills/nextjs-integration/scripts/setup-pages-router.sh
Files Created:
pages/_app.tsx - ClerkProvider wrapperpages/api/auth.ts - API route for auth callbackspages/sign-in/[[...index]].tsx - Sign-in pagepages/sign-up/[[...index]].tsx - Sign-up pagePages Router Features:
Copy Template Files:
# _app.tsx with ClerkProvider
cp ./skills/nextjs-integration/templates/pages-router/_app.tsx ./pages/_app.tsx
# Auth API route
cp ./skills/nextjs-integration/templates/pages-router/api/auth.ts ./pages/api/auth.ts
Configure middleware for route protection:
# Setup auth middleware with route matching
bash ./skills/nextjs-integration/scripts/configure-middleware.sh
Middleware Patterns:
/dashboard/*)Middleware Configuration:
// middleware.ts - Protects routes at the edge
import { authMiddleware } from "@clerk/nextjs";
export default authMiddleware({
publicRoutes: ["/", "/api/public"],
ignoredRoutes: ["/api/webhook"],
});
export const config = {
matcher: ['/((?!.+\\.[\\w]+$|_next).*)', '/', '/(api|trpc)(.*)'],
};
Use auth() helper in Server Components:
import { auth } from '@clerk/nextjs';
export default async function DashboardPage() {
const { userId } = auth();
if (!userId) {
redirect('/sign-in');
}
// Protected server component logic
const userData = await fetchUserData(userId);
return <div>Welcome {userData.name}</div>;
}
Server-Side Helpers:
auth() - Get current user sessioncurrentUser() - Get full user objectredirectToSignIn() - Redirect helperclerkClient - Server-side Clerk API clientUse React hooks in Client Components:
'use client';
import { useAuth, useUser } from '@clerk/nextjs';
export function UserProfile() {
const { userId, isLoaded, isSignedIn } = useAuth();
const { user } = useUser();
if (!isLoaded) return <div>Loading...</div>;
if (!isSignedIn) return <div>Please sign in</div>;
return <div>Hello {user.firstName}</div>;
}
Client-Side Hooks:
useAuth() - Authentication stateuseUser() - Current user datauseClerk() - Clerk instance methodsuseSignIn() - Sign-in flow controluseSignUp() - Sign-up flow control# 1. Install Clerk
bash ./skills/nextjs-integration/scripts/install-clerk.sh
# 2. Configure App Router
bash ./skills/nextjs-integration/scripts/setup-app-router.sh
# 3. Setup middleware
bash ./skills/nextjs-integration/scripts/configure-middleware.sh
# 4. Copy protected route example
cp ./skills/nextjs-integration/examples/protected-route.tsx ./app/dashboard/page.tsx
# 5. Copy server component auth example
cp ./skills/nextjs-integration/examples/server-component-auth.tsx ./app/profile/page.tsx
# 6. Start development server
npm run dev
Result: Fully configured Next.js App Router with Clerk authentication, protected routes, and sign-in/sign-up pages
# 1. Install Clerk
bash ./skills/nextjs-integration/scripts/install-clerk.sh
# 2. Configure Pages Router
bash ./skills/nextjs-integration/scripts/setup-pages-router.sh
# 3. Copy API route template
cp ./skills/nextjs-integration/templates/pages-router/api/auth.ts ./pages/api/auth.ts
# 4. Test authentication
npm run dev
Result: Pages Router setup with API route authentication and custom auth pages
Configure organization-based authentication:
// Server Component with organization context
import { auth } from '@clerk/nextjs';
export default async function TeamDashboard() {
const { orgId, userId } = auth();
if (!orgId) {
return <div>Please select an organization</div>;
}
const teamData = await fetchTeamData(orgId);
return <TeamView data={teamData} />;
}
Organization Features:
// App Router API route with auth
import { auth } from '@clerk/nextjs';
import { NextResponse } from 'next/server';
export async function GET() {
const { userId } = auth();
if (!userId) {
return new NextResponse('Unauthorized', { status: 401 });
}
const data = await fetchProtectedData(userId);
return NextResponse.json(data);
}
Dependencies:
@clerk/nextjs - Clerk Next.js SDKClerk Account:
Project Structure:
create-next-appapp/ directory (App Router) or pages/ directory (Pages Router)Environment Variables:
.env.local for local development.env.production for production (never commit secrets)Never Hardcode API Keys:
# ✅ CORRECT - Use environment variables
CLERK_SECRET_KEY=your_clerk_secret_key_here
# ❌ WRONG - Never commit secrets
const clerkSecret = "sk_test_abc123..." // DON'T DO THIS
Protect Sensitive Routes:
Secure API Routes:
// Always validate auth server-side
const { userId } = auth();
if (!userId) {
return new NextResponse('Unauthorized', { status: 401 });
}
Environment Variable Management:
.env.local for development (git-ignored).env.example with placeholders (safe to commit)Use App Router When:
Use Pages Router When:
Migration Path:
With Supabase:
With tRPC:
With Prisma:
With Vercel:
Middleware Not Running:
Sign-In Redirect Loop:
NEXT_PUBLIC_CLERK_SIGN_IN_URL matches routeServer Component Hydration:
Environment Variables Not Loading:
Plugin: clerk Version: 1.0.0 Category: Authentication Skill Type: Integration & Configuration
README.md (opens in a new tab)VALIDATION_SUMMARY.md (opens in a new tab)examples/client-component-auth.tsx (opens in a new tab)examples/protected-route.tsx (opens in a new tab)examples/server-component-auth.tsx (opens in a new tab)scripts/configure-middleware.sh (opens in a new tab)scripts/install-clerk.sh (opens in a new tab)scripts/setup-app-router.sh (opens in a new tab)scripts/setup-pages-router.sh (opens in a new tab)templates/app-router/layout.tsx (opens in a new tab)templates/app-router/middleware.ts (opens in a new tab)templates/pages-router/_app.tsx (opens in a new tab)templates/pages-router/api/auth.ts (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
10 skills in this plugin
/api-authenticationBackend API authentication patterns with Clerk JWT middleware and route protection. Use when building REST APIs, GraphQL APIs, protecting backend routes, implementing JWT validation, setting up Express middleware, or when user mentions API authentication, backend security, JWT tokens, or protected endpoints./auth-componentsPre-built and custom Clerk authentication component templates with theming and customization patterns. Use when building authentication UI, creating sign-in/sign-up pages, customizing Clerk components, implementing user buttons, theming auth flows, or when user mentions Clerk components, SignIn, SignUp, UserButton, auth UI, appearance customization, or authentication theming./billing-integrationClerk Billing and Stripe subscription management setup. Use when implementing subscriptions, configuring pricing plans, setting up billing, adding payment flows, managing entitlements, or when user mentions Clerk Billing, Stripe integration, subscription management, pricing tables, payment processing, or monetization./middleware-protectionRoute protection and authorization patterns for Clerk middleware. Use when implementing route guards, protecting API routes, configuring middleware matchers, setting up role-based access control, creating auth boundaries, or when user mentions middleware, route protection, auth guards, protected routes, public routes, matcher patterns, or authorization middleware./oauth-providersConfigure OAuth authentication providers for Clerk (Google, GitHub, Discord, Apple, Microsoft, Facebook, LinkedIn, Twitter, and 11+ more). Use when setting up social login, configuring OAuth providers, implementing authentication flows, generating redirect URLs, testing OAuth connections, or when user mentions Clerk OAuth, social authentication, provider setup, or multi-provider auth./organization-managementImplement Clerk multi-tenant organization features with RBAC, role-based access control, organization switching, member management, and tenant isolation. Use when building multi-tenant SaaS applications, implementing organization hierarchies, configuring custom roles and permissions, setting up organization-scoped data isolation, or when user mentions organizations, RBAC, multi-tenancy, roles, permissions, organization switcher, member management, or tenant isolation.npx claudepluginhub vanman2024/ai-dev-marketplace --plugin clerkOther plugins' skills for the same job