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.

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
2

Install the Auth0 Next.js SDK

3

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:
4

Create the Auth0 configuration

Create lib/auth0.ts:
lib/auth0.ts
5

Add middleware

Create middleware.ts in your project root:
middleware.ts
If you’re using a src/ directory, the middleware.ts file must be created inside the src/ directory.
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
6

Create Login, Logout and Profile Components

Create the components directory and files:
Add the component code:
7

Update your main page

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

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

Add styling

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

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

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:
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