Installation
Import the server API from the dedicated server package:Auth0Client Instance
Create a single instance ofAuth0Client to use throughout your application:
lib/auth0.ts
Core Methods
Session Management
getSession
Retrieve the current user’s session data
updateSession
Update session data for authenticated users
Token Management
getAccessToken
Get access tokens with automatic refresh
Session Stores
Configure stateless or stateful session storage
Route Protection
withPageAuthRequired
Protect pages and redirect unauthenticated users
App Router vs Pages Router
The SDK provides different method signatures depending on your Next.js routing strategy:Router Compatibility
| Method | App Router | Pages Router | Middleware |
|---|---|---|---|
getSession() | ✅ No args | ✅ (req) | ✅ (req) |
getAccessToken() | ✅ (options?) | ✅ (req, res, options?) | ✅ (req, res, options?) |
updateSession() | ✅ (session) | ✅ (req, res, session) | ✅ (req, res, session) |
withPageAuthRequired() | ✅ Supported | ✅ Supported | ❌ N/A |
Environment Variables
The SDK requires the following environment variables:Your Auth0 tenant domain (e.g.,
example.us.auth0.com)Your Auth0 application client ID
Your Auth0 application client secret
A 32-byte hex-encoded secret for encrypting cookies. Generate with:
Your application’s base URL (e.g.,
http://localhost:3000). If omitted, the SDK infers it from the request host at runtime.Next Steps
Auth0Client Configuration
Learn about all constructor options
Session Data
Understand the session structure