Skip to main content
The useUser hook provides access to the authenticated user’s profile and session state in client components. It uses SWR (Stale-While-Revalidate) for efficient data fetching and caching.

Import

This hook must be used in client components (marked with "use client" directive).

Signature

Return Value

The hook returns an object with the following properties:
User | null | undefined
The authenticated user object, null if not authenticated, or undefined while loading.The User type includes:
  • sub - User ID (subject)
  • name - Full name
  • nickname - Nickname
  • given_name - First name
  • family_name - Last name
  • picture - Profile picture URL
  • email - Email address
  • email_verified - Email verification status
  • org_id - Organization ID (if logged in through an organization)
  • Additional custom claims
boolean
true while the user data is being fetched, false otherwise.
Error | null
Error object if fetching user data failed, null otherwise.
() => void
Function to manually trigger a revalidation of the user data.

Basic Usage

app/profile/page.tsx

Usage with Auth0Provider

For optimal performance, wrap your application with Auth0Provider to provide initial user data:
app/layout.tsx
This eliminates the initial loading state when the page first renders.

Manual Revalidation

You can manually trigger a refresh of user data using the invalidate function:

Conditional Rendering

Error Handling

Understanding SWR Behavior

The useUser hook uses SWR (Stale-While-Revalidate) under the hood, which provides:
  • Event-driven revalidation: Data automatically revalidates when you:
    • Focus the browser tab
    • Reconnect to the internet
    • Mount the component
  • No background polling: The hook does not make continuous background requests unless explicitly configured
  • Cache-first approach: Returns cached data immediately, then revalidates if needed

Customizing SWR Behavior

While useUser doesn’t expose SWR configuration directly, you can control revalidation behavior globally via Auth0Provider:
app/layout.tsx

API Route

The useUser hook fetches data from the /auth/profile route (configurable via NEXT_PUBLIC_PROFILE_ROUTE environment variable). If you’re using custom routes, ensure your profile endpoint returns user data:
app/api/auth/profile/route.ts

TypeScript

Custom Environment Variables

You can customize the profile API route:
.env.local

See Also