Skip to main content
withPageAuthRequired is a higher-order component (HOC) that protects client-side pages by redirecting unauthenticated users to the login page. After login, users are automatically returned to the page they were trying to access.
This is the client-side version for React components. For server-side page protection, see withPageAuthRequired (Server).

Usage

Basic Protection

Custom Redirect Path

Custom Loading State

Custom Error Handling

Signature

Parameters

ComponentType<P & UserProps>
required
The React component to protect. The component will receive a user prop containing the authenticated user’s profile.
WithPageAuthRequiredOptions
Configuration options for the HOC.

Return Value

Returns a wrapped React component that:
  1. Checks if the user is authenticated using useUser()
  2. Redirects to login if not authenticated
  3. Shows the loading state while checking authentication
  4. Shows the error state if authentication check fails
  5. Renders the protected component with the user prop if authenticated

UserProps Type

The protected component receives a user prop with the following type:

How It Works

  1. Authentication Check: Uses useUser() to fetch the current user
  2. Redirect Logic: If not authenticated, redirects to /auth/login?returnTo=<current-page>
  3. Return Path: After login, Auth0 redirects back to the returnTo URL
  4. Loading State: Shows onRedirecting() while checking authentication
  5. Error State: Shows onError() if the profile fetch fails

Configuration

The login endpoint can be customized using the NEXT_PUBLIC_LOGIN_ROUTE environment variable:

Examples

Complete Protected Page

With TypeScript

Pages Router Example

App Router Example

Client vs Server Protection

Recommendation: Prefer server-side protection for better UX and SEO. Use client-side protection only for dynamic client components that can’t be server-rendered.

Notes

  • Requires Auth0Provider to be configured in your app
  • The protected component must be a client component (marked with "use client")
  • The user prop is automatically injected into your component
  • For server-side protection with better performance and SEO, use the server version