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 withAuth0Provider:
Pages Router
Wrap your_app.tsx with Auth0Provider:
getServerSideProps:
Signature
Props
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.Your application’s component tree.
User Type
TheUser type contains standard OIDC profile claims:
How It Works
Under the hood,Auth0Provider uses SWR to manage user state:
- If a
userprop is provided, it’s used as the initial data (no loading state) - The
useUser()hook fetches from/auth/profileand caches the result - All
useUser()calls across your app share the same cached user data - The cache automatically revalidates when the window regains focus or network reconnects
Configuration
The profile endpoint can be customized using theNEXT_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
Auth0Providercomponent must be marked with"use client"or used in a client component - Passing the
userprop from server-side prevents a loading flash on initial render - The provider automatically handles caching and revalidation of user data
- You only need one
Auth0Providerat the root of your application