Skip to main content

AI Prompt

Using AI to integrate Auth0? Add this prompt to Cursor, Windsurf, Copilot, Claude Code or your favourite AI-powered IDE to speed up development.
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.
1

Create a new project

Create a new Next.js project for this Quickstart
Open 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.
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 --legacy-peer-deps flag:
The --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.
3

Create project files

Create all necessary directories and files for Auth0 integration:
4

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 can choose to do this automatically by running a CLI command or do it manually via the Dashboard:
Run the following shell command on your project’s root directory to create an Auth0 app and generate a .env.local file:
5

Create the Auth0 configuration

Add the Auth0 client code to src/lib/auth0.ts:
src/lib/auth0.ts
6

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
7

Create Login, Logout and Profile Components

Add the component code to the files created in Step 3:
8

Update your main page

Replace src/app/page.tsx with:
src/app/page.tsx
9

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.
10

Add styling

Replace src/app/globals.css with modern Auth0-branded styling:
src/app/globals.css
11

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

If you see a 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:
  1. Generate a new secret using:
  1. Update your .env.local file:
  1. 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
  2. Restart your dev server:
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.
If clicking login takes you to a 404 page, check these common issues:
  1. Middleware location: Ensure src/middleware.ts exists in the correct location
  2. Middleware code: Verify the middleware matches the code in Step 6
  3. Restart server: After creating middleware, restart the dev server
  4. Check imports: Make sure import { auth0 } from "./lib/auth0" path is correct
If you see “Cannot find module ’@/components/LoginButton’” or similar errors:
  1. Verify files exist: Check that all files from Step 3 were created
  2. Check paths: Ensure components are in src/components/ directory
  3. Restart TypeScript: Press Cmd+Shift+P (Mac) or Ctrl+Shift+P (Windows) and run “TypeScript: Restart TS Server”
  4. Verify imports: Make sure you’re using @/components/* (not ~/components/*)

Advanced Usage

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:
If you’re experiencing 404 errors on these routes, ensure that:
  1. The middleware.ts file is created in the correct location (project root, or inside src/ if using a src/ directory)
  2. The middleware is properly configured with the matcher pattern shown in Step 5
  3. The development server was restarted after creating the middleware file
The Auth0 Next.js SDK v4 supports both App Router and Pages Router patterns. Here are some common server-side patterns:
app/protected/page.tsx
For client-side authentication state, use the useUser hook:
components/UserProfile.tsx
For API route protection, use the withApiAuthRequired method:
app/api/protected/route.ts