clerk-authentication
Guidelines for implementing Clerk authentication in Next.js applications with middleware, hooks, and security best practices
By mindrally · 636 installs
npx skills add mindrally/skills --skill clerk-authentication
Source repository · Upstream listing
Clerk Authentication
You are an expert in Clerk authentication implementation for Next.js applications. Follow these guidelines when integrating Clerk.
Core Principles
Implement defense in depth with multiple authentication layers
Verify authentication at every data access point, not just middleware
Protect server actions individually
Use Clerk's built in security features (HttpOnly cookies, CSRF protection)
Installation and Setup
Environment Variables
Provider Setup
App Router (app/layout.tsx)
With Custom Appearance
Middleware Configuration
Basic Middleware (middleware.ts)
Advanced Middleware with Role Based Access
Authentication in Server Components
Using auth()
Using currentUser()
Authentication in Client Components
useUser Hook
useAuth Hook
Server Actions Protection
Always protect server actions individually:
With Role Validation
API Route Protection
Route Handlers (App Router)
JWT Verification for External APIs
Organization Support
Custom Session Claims
Configure in Clerk Dashboard
Add custom claims via JWT Templates, then access them:
UI Components
Pre built Components
Security Best Practices
1. Defense in Depth
2. Protect All Server Actions
3. Avoid Client Side Only Protection
Error Handling
Testing
Common Anti Patterns to Avoid
1. Relying solely on middleware for protection
2. Not protecting server actions individually
3. Using client side auth checks for sensitive data
4. Exposing user data without ownership verification
5. Not validating organization membership for org scoped resources
6. Hardcoding role checks instead of using Clerk's RBAC
7. Not handling loading states in client components