Skip to main content
The getSession method retrieves the session data for the currently authenticated user.

Method Signatures

The method has different signatures depending on the routing context:

Parameters

IncomingMessage | NextApiRequest | NextRequest
The request object. Required for Pages Router and middleware, omitted for App Router.

Returns

Returns a Promise that resolves to:
  • SessionData object if the user is authenticated
  • null if no active session exists

SessionData Structure

Usage Examples

App Router

Pages Router

Middleware

middleware.ts

Accessing Session Data

User Profile

Token Information

Custom Claims

If you’ve added custom claims via hooks:

Session Lifecycle

Creation

Sessions are created after successful authentication via the /auth/callback route.

Expiration

Sessions expire based on:
  1. Absolute Duration: Maximum session lifetime (default: 3 days)
  2. Inactivity Duration: Time of inactivity before expiration (default: 1 day)
Configure in Auth0Client constructor:

Rolling Sessions

When rolling is enabled (default), sessions are automatically extended on each request:

Error Handling

getSession returns null when:
  • No session cookie exists
  • Session cookie is invalid or tampered
  • Session has expired
  • Session decryption fails

Important Notes

The getSession method returns a complete session object containing the user profile and all available tokens (access token, ID token, and refresh token when present).For API access with automatic token refresh, use getAccessToken instead.
In middleware, you must pass the request object to getSession(request) to ensure session updates can be read within the same request.
Session data is encrypted in cookies by default. For database-backed sessions, configure a custom session store.

updateSession

Update session data

getAccessToken

Get access tokens with refresh