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 aPromise that resolves to:
SessionDataobject if the user is authenticatednullif 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:- Absolute Duration: Maximum session lifetime (default: 3 days)
- Inactivity Duration: Time of inactivity before expiration (default: 1 day)
Auth0Client constructor:
Rolling Sessions
Whenrolling 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.Session data is encrypted in cookies by default. For database-backed sessions, configure a custom session store.
Related Methods
updateSession
Update session data
getAccessToken
Get access tokens with refresh