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
- Go to the Auth0 Dashboard
- Create a new application or select an existing one
- Choose Regular Web Application as the application type
- 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 filelib/auth0.ts (or lib/auth0.js) in your project:
lib/auth0.ts
Step 4: Add Authentication Middleware
Authentication requests are intercepted at the network boundary. Choose the setup for your Next.js version:- Next.js 15
- Next.js 16
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 Router
- Pages Router
app/page.tsx
Step 6: Test Your Application
1
Start the development server
http://localhost:3000.2
Test the login flow
- Click Log in or Sign up
- You’ll be redirected to Auth0’s login page
- Enter your credentials or sign up
- 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 theAuth0Provider:
app/layout.tsx
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
Invalid state error
Invalid state error
This usually happens when cookies are blocked or the
AUTH0_SECRET has changed. Make sure:- Cookies are enabled in your browser
- The
AUTH0_SECRETmatches across restarts - You’re not mixing HTTP and HTTPS during development
Redirect URI mismatch
Redirect URI mismatch
Ensure your callback URL is registered in Auth0:
- Go to your Auth0 application settings
- Add
http://localhost:3000/auth/callbackto Allowed Callback URLs - For production, add your production callback URL
Session not persisting
Session not persisting
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.