Skip to main content
This guide will get you from zero to a working authentication flow in your Next.js application.

Prerequisites

Before you begin, make sure you have:
  • Node.js 20 LTS or newer installed
  • A Next.js application (14.2.35 or newer)
  • An Auth0 account (free tier works)

Step 1: Install the SDK

Install the Auth0 Next.js SDK using your preferred package manager:

Step 2: Configure Environment Variables

Create a .env.local file in your project root and add the following variables:
.env.local
1

Get Auth0 credentials

  1. Go to the Auth0 Dashboard
  2. Create a new application or select an existing one
  3. Choose Regular Web Application as the application type
  4. Copy the Domain, Client ID, and Client Secret from the application settings
2

Generate AUTH0_SECRET

Generate a random 32-byte hex string for encrypting session cookies:
Copy the output and use it as your AUTH0_SECRET.
3

Configure callback URLs

In your Auth0 application settings, add the following URLs:
  • Allowed Callback URLs: http://localhost:3000/auth/callback
  • Allowed Logout URLs: http://localhost:3000
For production deployments, update APP_BASE_URL to your production domain and register the production callback URLs in Auth0.

Step 3: Create the Auth0 Client

Create a file lib/auth0.ts (or lib/auth0.js) in your project:
lib/auth0.ts
This creates an Auth0 client instance that will be used throughout your application.

Step 4: Add Authentication Middleware

Authentication requests are intercepted at the network boundary. Choose the setup for your Next.js version:
Create a middleware.ts file in the root of your project:
middleware.ts
If you’re using a src/ directory, place the middleware.ts file inside the src/ directory.
The broad middleware matcher is essential for rolling sessions and security features. See the Session Configuration guide for more details.

Step 5: Add Login and Logout

Update your home page to include login and logout functionality:
app/page.tsx
Always use <a> tags for auth routes instead of Next.js <Link> components to ensure proper server-side navigation.

Step 6: Test Your Application

1

Start the development server

Your app should now be running at http://localhost:3000.
2

Test the login flow

  1. Click Log in or Sign up
  2. You’ll be redirected to Auth0’s login page
  3. Enter your credentials or sign up
  4. You’ll be redirected back to your app, now logged in
3

Test the logout flow

Click Log out to sign out. You’ll be logged out of Auth0 and redirected back to your app.

Access User Information

Now that authentication is working, you can access user information throughout your app:

In Server Components (App Router)

In Client Components

First, wrap your app with the Auth0Provider:
app/layout.tsx
Then use the useUser hook in client components:

Next Steps

You now have a fully working authentication system! Here are some next steps:

Protect Pages

Learn how to protect entire pages and routes

Get Access Tokens

Call external APIs with access tokens

Customize Sessions

Configure session duration and storage

Advanced Features

Explore DPoP, MFA, and MRRT

Troubleshooting

This usually happens when cookies are blocked or the AUTH0_SECRET has changed. Make sure:
  • Cookies are enabled in your browser
  • The AUTH0_SECRET matches across restarts
  • You’re not mixing HTTP and HTTPS during development
Ensure your callback URL is registered in Auth0:
  • Go to your Auth0 application settings
  • Add http://localhost:3000/auth/callback to Allowed Callback URLs
  • For production, add your production callback URL
Check that your middleware is properly configured and the matcher pattern is correct. The middleware must run on every request for rolling sessions to work.
Need more help? Check the Troubleshooting guide.