Add authentication to a Next.js application
Installs and configures Auth.js v5 authentication in Next.js applications with OAuth and credential providers.
/plugin marketplace add davepoon/buildwithclaude/plugin install nextjs-expert@buildwithclaudeYou are adding authentication to a Next.js application using Auth.js (NextAuth.js v5).
Check if authentication already exists:
auth.ts or auth.config.tsnext-auth in package.jsonIdentify database setup:
Use AskUserQuestion to clarify:
npm install next-auth@beta @auth/prisma-adapter
If using credentials:
npm install bcryptjs
npm install -D @types/bcryptjs
Apply the auth-patterns skill and create:
auth.ts - Main auth configuration with:
app/api/auth/[...nextauth]/route.ts - Route handler
Create middleware.ts with:
If using Prisma, add/update models:
Create/update .env.local:
AUTH_SECRET=generated-secret
AUTH_URL=http://localhost:3000
GITHUB_CLIENT_ID=...
GITHUB_CLIENT_SECRET=...
| File | Purpose |
|---|---|
auth.ts | Auth configuration |
app/api/auth/[...nextauth]/route.ts | NextAuth route handler |
middleware.ts | Route protection |
app/login/page.tsx | Login page |
app/register/page.tsx | Registration page (if credentials) |
components/login-form.tsx | Login form component |
components/oauth-buttons.tsx | OAuth sign-in buttons |
components/user-menu.tsx | User dropdown menu |
app/providers.tsx | SessionProvider wrapper |
types/next-auth.d.ts | Extended types |
lib/auth-helpers.ts | Auth utility functions |
AUTH_SECRET from environmenthttpOnly cookies