Prerequisites
Before installing the Auth0 Next.js SDK, ensure you have:- Node.js 20 LTS or newer LTS version
- A Next.js application (version 14.2.35 or later)
- An Auth0 account and application
The SDK supports Next.js 14, 15, and 16, with both the App Router and Pages Router.
Install the SDK
Install the@auth0/nextjs-auth0 package using your preferred package manager:
Configure Environment Variables
Create a.env.local file in the root of your project and add the following environment variables:
.env.local
Required Variables
Your Auth0 tenant domain (e.g.,
example.us.auth0.com or https://example.us.auth0.com). Find this in your Auth0 Dashboard under your application settings.Your Auth0 application’s client ID. This can be found in the Auth0 Dashboard when you create a Regular Web Application.
Your Auth0 application’s client secret. This is shown in the Auth0 Dashboard alongside the client ID.
A 32-byte, hex-encoded secret used for encrypting session cookies. Generate one using:
The URL where your application is running. For local development, this is typically
http://localhost:3000. For production, use your production URL.You can omit
APP_BASE_URL to let the SDK infer it from the request host at runtime. This is useful for preview deployments on platforms like Vercel or Netlify.Configure Auth0 Application Settings
In your Auth0 Dashboard, configure your application with the following URLs:Create a Regular Web Application
If you haven’t already, create a new application and select Regular Web Application as the application type.
Add Callback URLs
Add your callback URL to the Allowed Callback URLs field:For production, add your production callback URL:
Dynamic Base URLs (Preview Deployments)
For preview environments on platforms like Vercel or Netlify, you can omit theAPP_BASE_URL environment variable. The SDK will automatically infer the base URL from the incoming request host.
Auth0’s Allowed Callback URLs serve as the security boundary. If the inferred host is not registered in your Auth0 application, Auth0 will reject the authorization request.
Static Base URL (Recommended for Production)
For production environments with a stable domain, explicitly set theAPP_BASE_URL:
lib/auth0.ts
Dynamic Base URL (Preview Environments)
For dynamic preview URLs, omit theappBaseUrl configuration:
lib/auth0.ts
When relying on dynamic base URLs in production, the SDK enforces secure cookies. Setting
AUTH0_COOKIE_SECURE=false will throw an InvalidConfigurationError.Optional Environment Variables
You can configure additional behavior using these optional environment variables:.env.local
Base Path Configuration
If your Next.js application uses a base path (configured viabasePath in next.config.js), set the NEXT_PUBLIC_BASE_PATH environment variable:
.env.local
/dashboard/auth/login, /dashboard/auth/callback, etc.
Do not use
NEXT_PUBLIC_BASE_PATH with an APP_BASE_URL that contains a path component. Set APP_BASE_URL to the root URL (e.g., https://example.com) and use NEXT_PUBLIC_BASE_PATH for the base path.