npx skills add ...
npx skills add auth0/agent-skills --skill auth0-nextjs
Use when adding Auth0 login, logout, protected pages, or middleware to a Next.js application. Supports App Router and Pages Router with @auth0/nextjs-auth0 — use even if the user says "add login to my Next.js app" or "protect my Next.js routes".
npx skills add auth0/agent-skills --skill auth0-nextjs
Add authentication to Next.js applications using @auth0/nextjs-auth0. Supports both App Router and Pages Router.
auth0-quickstart skill firstauth0-react for Vite/CRA SPAsauth0-react-native for iOS/AndroidFor automated setup with Auth0 CLI, see Setup Guide [blocked] for complete scripts.
For manual setup:
Create .env.local:
Generate secret: openssl rand -hex 32
Important: Add .env.local to .gitignore
Detect project structure first: Check whether the project uses a src/ directory (i.e. src/app/ or src/pages/ exists). This determines where to place files:
src/: src/lib/auth0.ts, src/middleware.ts (or src/proxy.ts for Next.js 16)src/: lib/auth0.ts, middleware.ts (or proxy.ts for Next.js 16)Create lib/auth0.ts (or src/lib/auth0.ts if using the src/ convention):
Middleware Configuration (Next.js 15 vs 16):
Next.js 15 - Create middleware.ts (at project root, or src/middleware.ts if using src/):
Next.js 16 - You have two options:
Option 1: Use middleware.ts (same as Next.js 15, same src/ placement rules):
Option 2: Use proxy.ts (at project root, or src/proxy.ts if using src/):
This automatically creates endpoints:
/auth/login - Login/auth/logout - Logout/auth/callback - OAuth callback/auth/profile - User profileNote: In v4, wrapping with <Auth0Provider> is optional. Only needed if you want to pass an initial user during server rendering to useUser().
App Router - Optionally wrap app in app/layout.tsx:
Pages Router - Optionally wrap app in pages/_app.tsx:
Client Component (works in both routers):
Start your dev server:
Visit http://localhost:3000 and test the login flow.
| Mistake | Fix |
|---|---|
| Using v3 environment variables | v4 uses APP_BASE_URL and AUTH0_DOMAIN (not AUTH0_BASE_URL or AUTH0_ISSUER_BASE_URL) |
| Forgot to add callback URL in Auth0 Dashboard | Add /auth/callback to Allowed Callback URLs (e.g., http://localhost:3000/auth/callback) |
| Missing middleware configuration | v4 requires middleware to mount auth routes - create middleware.ts (Next.js 15+16) or proxy.ts (Next.js 16 only) with auth0.middleware() |
| Wrong route paths | v4 uses /auth/login not /api/auth/login - routes drop the /api prefix |
| Missing or weak AUTH0_SECRET | Generate secure secret with openssl rand -hex 32 and store in .env.local |
| Using .env instead of .env.local | Next.js requires .env.local for local secrets, and .env.local should be in .gitignore |
| App created as SPA type in Auth0 | Must be Regular Web Application type for Next.js |
| Using removed v3 helpers | v4 removed withPageAuthRequired and withApiAuthRequired - use getSession() instead |
| Using useUser in Server Component | useUser is client-only, use auth0.getSession() for Server Components |
| AUTH0_DOMAIN includes https:// | v4 AUTH0_DOMAIN should be just the domain (e.g., example.auth0.com), no scheme |
auth0-quickstart - Basic Auth0 setupauth0-migration - Migrate from another auth providerauth0-mfa - Add Multi-Factor Authenticationauth0-cli - Manage Auth0 resources from the terminalV4 Setup:
src/ convention: check if src/app/ or src/pages/ exists — place all files inside src/ if solib/auth0.ts (or src/lib/auth0.ts) with Auth0Client instancemiddleware.ts (or src/middleware.ts) with middleware() functionmiddleware.ts with middleware() OR proxy.ts with proxy() function (same src/ rules)<Auth0Provider> for SSR userClient-Side Hooks:
useUser() - Get user in client componentsuser - User profile objectisLoading - Loading stateServer-Side Methods:
auth0.getSession() - Get session in Server Components/API routes/middlewareauth0.getAccessToken() - Get access token for calling APIsCommon Use Cases:
/auth/login and /auth/logout paths (see Step 5)