giuseppe-trisciuoglio/developer-kit/nextjs-authentication
Claude Code and agent skill by giuseppe-trisciuoglio/developer-kit
- community
Checking that the SKILL.md file is still in the repository.
Provides authentication implementation patterns for Next.js 15+ App Router using Auth.js 5 (NextAuth.js). Use when setting up authentication flows, implementing protected routes, managing sessions in Server Components and Server Actions, configuring OAuth providers, implementing role-based access control, or handling sign-in/sign-out flows in Next.js applications.
Install
npx vskill@latest install giuseppe-trisciuoglio/developer-kit/nextjs-authenticationSKILL.md
The same SKILL.md was published earlier as giuseppe-trisciuoglio/developer-kit/nextjs-authentication.
Next.js Authentication
Overview
Provides authentication implementation patterns for Next.js 15+ App Router using Auth.js 5 (NextAuth.js), covering the complete authentication lifecycle from initial setup to production-ready role-based access control implementations.
When to Use
- Setting up Auth.js 5 from scratch or adding OAuth providers
- Implementing protected routes with Middleware
- Handling authentication in Server Components and Server Actions
- Implementing role-based access control (RBAC)
- Creating credential-based or OAuth sign-in/sign-out flows
Instructions
1. Install Dependencies
Install Auth.js v5 (beta) for Next.js App Router:
2. Configure Environment Variables
Create .env.local with required variables:
Generate AUTH_SECRET with:
3. Create Auth Configuration
Create auth.ts in the project root with providers and callbacks:
4. Create API Route Handler
Create app/api/auth/[...nextauth]/route.ts:
5. Add Middleware for Route Protection
Create middleware.ts in the project root:
6. Access Session in Server Components
Use the auth() function to access session in Server Components:
7. Secure Server Actions
Always verify authentication in Server Actions before mutations:
8. Handle Sign-In/Sign-Out
Create a login page with server action:
For client-side sign-out:
9. Implement Role-Based Access
Check roles in Server Components:
10. Extend TypeScript Types
Create types/next-auth.d.ts for type-safe sessions:
Examples
Example 1: Complete Protected Dashboard
Input: User needs a dashboard accessible only to authenticated users
Implementation:
Output: Dashboard renders only for authenticated users, with their specific data.
Example 2: Role-Based Admin Panel
Input: Admin panel should be accessible only to users with "admin" role
Implementation:
Output: Only admin users see the panel; others get 401 error.
Example 3: Secure Server Action with Form
Input: Form submission should only work for authenticated users
Implementation:
Output: Todo created only for authenticated user; unauthorized requests throw error.
Best Practices
- Use Server Components by default - Access session directly without client-side JavaScript
- Minimize Client Components - Only use
useSession()for reactive session updates - Cache session checks - Use React's
cache()for repeated lookups in the same render - Middleware for optimistic checks - Redirect quickly, but always re-verify in Server Actions
- Treat Server Actions like API endpoints - Always authenticate before mutations
- Never hardcode secrets - Use environment variables for all credentials
- Implement proper error handling - Return appropriate HTTP status codes
- Use TypeScript type extensions - Extend NextAuth types for custom fields
- Separate auth logic - Create a DAL (Data Access Layer) for consistent checks
- Test authentication flows - Mock
auth()function in unit tests
Constraints and Warnings
Critical Limitations
- Middleware runs on Edge runtime - Cannot use Node.js APIs like database drivers
- Server Components cannot set cookies - Use Server Actions for cookie operations
- Session callback timing - Only called on session creation/access, not every request
Common Mistakes
Security Considerations
- Always verify authentication in Server Actions - middleware alone is not enough
- Use
unauthorized()for unauthenticated access,redirect()for other cases - Store sensitive tokens in
httpOnlycookies - Validate all user input before processing
- Use HTTPS in production
- Set appropriate cookie
sameSiteattributes
References
- references/authjs-setup.md - Complete Auth.js 5 setup guide with Prisma/Drizzle adapters
- references/oauth-providers.md - Provider-specific configurations (GitHub, Google, Discord, Auth0, etc.)
- references/database-adapter.md - Database session management with Prisma, Drizzle, and custom adapters
- references/testing-patterns.md - Testing authentication flows with Vitest and Playwright
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingplugins/developer-kit-typescript/skills/nextjs-authentication/SKILL.md
Popularity
- Repository stars
- 304
- Repository forks
- 34
- 7-day trend score
- 29
Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 13 Jul 2026, 4 months ago.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/giuseppe-trisciuoglio/developer-kit/nextjs-authentication)