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 aPromise<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
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
Related Methods
getSession
Retrieve current session
Session Stores
Configure session storage