auth0-nextjs

Add Auth0 authentication with login, logout, and protected routes to Next.js applications.

23|71|Updated Jul 8, 2025
One-click install
npx skills add https://github.com/auth0/docs-v2 --skill auth0-nextjs-auth0
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: auth0-nextjs
Source: https://github.com/auth0/docs-v2/tree/main/main/.mintlify/skills/auth0-nextjs
Command: npx skills add https://github.com/auth0/docs-v2 --skill auth0-nextjs-auth0

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Next.js projects require standardized login, logout, protected pages, and middleware hooks but configuring Auth0 sessions across App Router and Pages Router can be error prone without guidance.

Core Features & Use Cases

  • Auth0Client provisioning: sets up a reusable Auth0Client instance wired to AUTH0_DOMAIN, AUTH0_CLIENT_ID/SECRET, AUTH0_SECRET, and APP_BASE_URL environment values so APIs and server components can acquire sessions.
  • Middleware-driven routes: provides middleware or proxy templates that mount /auth/login, /auth/logout, /auth/callback, and /auth/profile endpoints while matching every non-static path.
  • Router support and UI hooks: explains optional Auth0Provider wrapping for App Router layouts or Pages _app along with client-only useUser hooks to render login status and profile components.
  • Use Case: any Next.js app that needs to guard pages, API routes, or middleware boundaries can follow this workflow to add Auth0 login flows and user context without reinventing setup steps.

Quick Start

Install @auth0/nextjs-auth0, define the required Auth0 environment secrets, and add the middleware entry point to enable login/logout routes in your Next.js project.

Frequently Asked Questions about auth0-nextjs

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I add Auth0 authentication to a Next.js app?

Auth0 authentication in Next.js works by configuring an Auth0Client with domain and secret environment variables, then setting up middleware to intercept routes. This creates session-aware boundaries that guard pages, APIs, and client components.

How do I set up protected routes and middleware with Auth0 in Next.js?

Protected routes are set up by adding a middleware or proxy template that mounts /auth/login, /auth/logout, and /auth/callback endpoints. This middleware matches every non-static path to enforce session-aware access across your application.

Does Auth0 work with both the Next.js App Router and Pages Router?

Yes, Auth0 works with both the App Router and Pages Router. You can wrap App Router layouts or Pages _app with Auth0Provider to expose server-fetched user context and use client hooks to render login status and profile components.

What environment variables do I need to configure for Next.js Auth0 session management?

Next.js Auth0 session management requires AUTH0_DOMAIN, AUTH0_CLIENT_ID, AUTH0_CLIENT_SECRET, AUTH0_SECRET, and APP_BASE_URL environment variables. These values wire the reusable Auth0Client so server components and APIs can acquire user sessions.

Can I use client components to display the Auth0 user profile in Next.js?

Yes, you can display the Auth0 user profile by wrapping your application with Auth0Provider and using client-only useUser hooks. This allows client components to render login status and profile information using server-fetched user context.

Why do I need middleware to handle Auth0 login flows in Next.js?

Middleware is needed to handle Auth0 login flows because it intercepts incoming requests and mounts the required /auth/login, /auth/logout, and /auth/callback endpoints. This ensures every non-static path is properly guarded by session-aware authentication.