Use AI to integrate Auth0
Use AI to integrate Auth0
If you use an AI coding assistant like Claude Code, Cursor, or GitHub Copilot, you can add Auth0 authentication automatically in minutes using agent skills.Install:Then ask your AI assistant:Your AI assistant will automatically create your Auth0 application, fetch credentials, install
@auth0/nextjs-auth0, create API routes, and set up environment variables. Full agent skills documentation →Prerequisites: Before you begin, ensure you have the following installed:Next.js Version Compatibility: This quickstart uses Next.js 15 which is fully supported by the Auth0 SDK. Next.js 16 is also compatible but requires the
--legacy-peer-deps flag during installation (see Step 2 for details).Get Started
This quickstart demonstrates how to add Auth0 authentication to a Next.js application. You’ll build a full-stack web application with server-side rendering, secure login functionality, and protected routes using the Auth0 Next.js SDK.Create a new project
Create a new Next.js project for this QuickstartOpen the project
We’re using
create-next-app@15 to create a Next.js 15 project, which is fully supported by the Auth0 SDK. If you prefer to use Next.js 16 or already have a Next.js 16 project, you’ll need to use --legacy-peer-deps when installing the Auth0 SDK in Step 2.Install the Auth0 Next.js SDK
For Next.js 16 users: If you’re using Next.js 16 (or upgraded to it), install with the The
--legacy-peer-deps flag:--legacy-peer-deps flag is needed because Next.js 16 support is pending in the SDK. The SDK works correctly with Next.js 16 using this flag.Setup your Auth0 App
Next up, you need to create a new app on your Auth0 tenant and add the environment variables to your project.You have three options to set up your Auth0 app: use the Quick Setup tool (recommended), run a CLI command, or configure manually via the Dashboard:
- Quick Setup (recommended)
- CLI
- Dashboard
Create an Auth0 App and copy the pre-filled
.env file with the right configuration values.Add middleware
Add the middleware code to
src/middleware.ts:src/middleware.ts
Since we’re using a
src/ directory, the middleware.ts file is created inside src/. If you’re not using a src/ directory, create it in the project root instead.This middleware automatically mounts the following authentication routes:
/auth/login- Login route/auth/logout- Logout route/auth/callback- Callback route/auth/profile- User profile route/auth/access-token- Access token route/auth/backchannel-logout- Backchannel logout route
Update layout with Auth0Provider (OPTIONAL)
Update
src/app/layout.tsx to wrap your app with the Auth0Provider:src/app/layout.tsx
In v4, the Auth0Provider is optional. You only need it if you want to pass an initial user during server rendering to be available to the
useUser() hook.Run your app
Your app will be available at http://localhost:3000. The Auth0 SDK v4 automatically mounts authentication routes at
/auth/* (not /api/auth/* like in v3).CheckpointYou should now have a fully functional Auth0 login page running on your localhost
Troubleshooting
JWEDecryptionFailed Error
JWEDecryptionFailed Error
If you see a The secret must be exactly 32 bytes (64 hexadecimal characters). The error occurs when the app tries to decrypt an existing session cookie that was encrypted with a different secret.
JWEDecryptionFailed: decryption operation failed error, this is caused by either an invalid AUTH0_SECRET or an old session cookie encrypted with a different secret.Solution:- Generate a new secret using:
- Update your
.env.localfile:
- Clear your browser cookies for
localhost:3000:- Chrome/Edge: Press
F12→ Application tab → Cookies → Delete all cookies for localhost - Firefox: Press
F12→ Storage tab → Cookies → Delete all cookies for localhost - Safari: Develop menu → Show Web Inspector → Storage tab → Cookies → Delete all
- Chrome/Edge: Press
- Restart your dev server:
404 Error on /auth/login
404 Error on /auth/login
If clicking login takes you to a 404 page, check these common issues:
- Middleware location: Ensure
src/middleware.tsexists in the correct location - Middleware code: Verify the middleware matches the code in Step 6
- Restart server: After creating middleware, restart the dev server
- Check imports: Make sure
import { auth0 } from "./lib/auth0"path is correct
Module Not Found Errors
Module Not Found Errors
If you see “Cannot find module ’@/components/LoginButton’” or similar errors:
- Verify files exist: Check that all files from Step 3 were created
- Check paths: Ensure components are in
src/components/directory - Restart TypeScript: Press
Cmd+Shift+P(Mac) orCtrl+Shift+P(Windows) and run “TypeScript: Restart TS Server” - Verify imports: Make sure you’re using
@/components/*(not~/components/*)
Advanced Usage
Important v4 Changes
Important v4 Changes
This quickstart uses Auth0 Next.js SDK v4, which has significant changes from v3:
- No dynamic route handlers needed - Authentication routes are auto-mounted by middleware
- Simplified client setup -
new Auth0Client()reads environment variables automatically - New route paths - Routes are at
/auth/*instead of/api/auth/* - Required middleware - All authentication functionality goes through middleware
- Use
<a>tags - Navigation must use<a href="/auth/login">instead of buttons with onClick
Authentication Routes
The SDK automatically mounts these routes via middleware:| Route | Purpose |
|---|---|
/auth/login | Initiate login |
/auth/logout | Logout user |
/auth/callback | Handle Auth0 callback |
/auth/profile | Get user profile |
/auth/access-token | Get access token |
/auth/backchannel-logout | Handle backchannel logout |
If you’re experiencing 404 errors on these routes, ensure that:
- The
middleware.tsfile is created in the correct location (project root, or insidesrc/if using asrc/directory) - The middleware is properly configured with the matcher pattern shown in Step 5
- The development server was restarted after creating the middleware file
Server-Side Authentication
Server-Side Authentication
The Auth0 Next.js SDK v4 supports both App Router and Pages Router patterns. Here are some common server-side patterns:
- App Router - Server Component
- App Router - API Route
- Pages Router - Page
app/protected/page.tsx
Client-Side Authentication
Client-Side Authentication
For client-side authentication state, use the
useUser hook:components/UserProfile.tsx
Protecting API Routes
Protecting API Routes
For API route protection, use the
withApiAuthRequired method:app/api/protected/route.ts