Skip to main content
Auth0Provider is a React component that provides Auth0 authentication context to your application. It wraps your application and enables the use of client-side hooks like useUser.

Usage

App Router

Wrap your root layout with Auth0Provider:

Pages Router

Wrap your _app.tsx with Auth0Provider:
Then pass the user in getServerSideProps:

Signature

Props

User
The authenticated user object from the server-side session. This provides initial user data to avoid a loading state on first render.If not provided, useUser() will fetch the user client-side on mount.
React.ReactNode
required
Your application’s component tree.

User Type

The User type contains standard OIDC profile claims:

How It Works

Under the hood, Auth0Provider uses SWR to manage user state:
  1. If a user prop is provided, it’s used as the initial data (no loading state)
  2. The useUser() hook fetches from /auth/profile and caches the result
  3. All useUser() calls across your app share the same cached user data
  4. The cache automatically revalidates when the window regains focus or network reconnects

Configuration

The profile endpoint can be customized using the NEXT_PUBLIC_PROFILE_ROUTE environment variable:

Example: Complete Setup

1. Create Auth0 Client Instance

2. Wrap App with Provider (App Router)

3. Use in Components

Notes

  • The Auth0Provider component must be marked with "use client" or used in a client component
  • Passing the user prop from server-side prevents a loading flash on initial render
  • The provider automatically handles caching and revalidation of user data
  • You only need one Auth0Provider at the root of your application