Skip to main content
The updateSession method updates the session data for the currently authenticated user. An error is thrown if the user does not have an active session.

Method Signatures

Parameters

IncomingMessage | NextApiRequest | NextRequest
The request object (Pages Router and middleware only)
ServerResponse | NextApiResponse | NextResponse
The response object (Pages Router and middleware only)
SessionData
required
The updated session data to persist

Returns

Returns a Promise<void>. The method completes when the session has been updated.

Usage Examples

App Router

Pages Router

Middleware

middleware.ts

Common Use Cases

Adding Custom Claims

Tracking User Activity

Storing Feature Flags

Updating User Metadata

Important Notes

Session updates are temporary. Any changes made via updateSession will be overwritten when the user re-authenticates and obtains a new session from Auth0.For persistent user data, update the user profile in Auth0 via the Management API.
Cannot be used in Server Components. The updateSession method requires the ability to set cookies, which is not possible in Server Components.Use Route Handlers or Server Actions instead.
The internal property containing session creation timestamp is preserved automatically. You don’t need to manually include it:
In middleware, you must pass both the request and response objects to ensure session updates can be read within the same request.

Error Handling

The method throws an error when:
  • User has no active session
  • Session data is missing or invalid

Pages Router Middleware Integration

When using Pages Router and reading session updates in the same request:
middleware.ts

Session Storage

Session updates are persisted differently based on your session store configuration:

Stateless Sessions (Default)

Updates are encrypted and stored in cookies:

Stateful Sessions

Updates are stored in your configured database:
lib/auth0.ts
See Session Stores for implementation details.

getSession

Retrieve current session

Session Stores

Configure session storage