Skip to main content

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

string
required
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.
string
required
Your Auth0 application’s client ID. This can be found in the Auth0 Dashboard when you create a Regular Web Application.
string
required
Your Auth0 application’s client secret. This is shown in the Auth0 Dashboard alongside the client ID.
string
required
A 32-byte, hex-encoded secret used for encrypting session cookies. Generate one using:
Keep this secret secure and never commit it to version control. Use different secrets for different environments.
string
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:
1

Create a Regular Web Application

If you haven’t already, create a new application and select Regular Web Application as the application type.
2

Add Callback URLs

Add your callback URL to the Allowed Callback URLs field:
For production, add your production callback URL:
3

Add Logout URLs

Add your logout URL to the Allowed Logout URLs field:
For production:
When using dynamic preview environments (Vercel, Netlify), ensure each preview URL is registered in your Auth0 application, or configure wildcard URLs if supported by your Auth0 plan.

Dynamic Base URLs (Preview Deployments)

For preview environments on platforms like Vercel or Netlify, you can omit the APP_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.
For production environments with a stable domain, explicitly set the APP_BASE_URL:
lib/auth0.ts

Dynamic Base URL (Preview Environments)

For dynamic preview URLs, omit the appBaseUrl 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 via basePath in next.config.js), set the NEXT_PUBLIC_BASE_PATH environment variable:
.env.local
This will mount authentication routes at /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.

Verify Installation

To verify your installation is working correctly, you can check that the SDK can be imported:

Next Steps

Now that you’ve installed and configured the SDK, proceed to the Quickstart to implement authentication in your Next.js application.