Session Data Structure
The session contains all authentication state for the current user:Storage Strategies
Stateless Sessions (Default)
Session data is encrypted and stored entirely in cookies. This is the default mode and requires no external dependencies. Advantages:- Zero external dependencies
- Simple deployment
- Automatic scaling (no shared state)
- Fast session retrieval
- Cookie size limits (~4KB per cookie)
- Cannot revoke sessions server-side
- All session data travels with every request
Stateful Sessions
Session data is stored in an external database, with only a session ID stored in the cookie. Advantages:- No cookie size limitations
- Server-side session revocation
- Reduced bandwidth (small cookie)
- Centralized session management
- Requires external data store
- Additional latency for database queries
- More complex infrastructure
For database session examples, see Database Sessions in the documentation.
Rolling Sessions
Rolling sessions extend the session lifetime with each request, up to an absolute maximum.- Active users remain authenticated as long as they use the app within the inactivity window
- Inactive users are automatically logged out after the inactivity duration
- Maximum lifetime prevents indefinite sessions
rolling: false):
- Session expires after
absoluteDurationregardless of activity - Users must re-login after the fixed duration
- More predictable but less user-friendly
The SDK’s middleware automatically touches sessions on every request when rolling sessions are enabled, extending the session lifetime. This is why the middleware matcher should include all protected routes.
Session Cookie Configuration
Cookie Attributes
Cookie Attributes Explained
Accessing Sessions
Server Components (App Router)
API Routes (App Router)
Pages Router
Client Components
The
useUser() hook fetches user data from the /auth/profile route, which reads from the server-side session.Updating Sessions
You can modify session data after authentication:Session updates are overwritten when the user re-authenticates or when tokens are refreshed. For persistent user metadata, store it in your database or use Auth0’s user metadata feature.
Token Refresh
When a refresh token is available, the SDK automatically refreshes expired access tokens:Session Security
Encryption
All session data is encrypted using JWE (JSON Web Encryption) with theAUTH0_SECRET:
Session Lifecycle
Best Practices
- Use strong secrets: Generate with
openssl rand -hex 32 - Enable HTTPS: Set
secure: truein production - Configure SameSite: Use
'lax'or'strict'for CSRF protection - Set reasonable durations: Balance security and user experience
- Monitor session size: Keep under 4KB for cookie-based sessions
- Implement logout: Clear sessions when users log out
- Use refresh tokens: Enable offline_access scope for long-lived sessions
- Handle token expiration: Refresh tokens before they expire
Multi-Resource Refresh Tokens (MRRT)
When calling multiple APIs, the SDK stores separate access tokens per audience:Learn more about Multi-Resource Refresh Tokens in the documentation.
Session Debugging
To inspect session contents during development:Next Steps
- Explore Authentication Routes that interact with sessions
- Learn about Authentication Flow that creates sessions
- Configure Session Settings for your needs