Skip to main content
After successful authentication, the Auth0 Next.js SDK creates and manages a user session. The SDK supports two session storage strategies: stateless (cookie-based) and stateful (database-backed).

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
Limitations:
  • Cookie size limits (~4KB per cookie)
  • Cannot revoke sessions server-side
  • All session data travels with every request
Configuration:
Storage mechanism: The SDK automatically chunks large sessions across multiple cookies if needed.
If your session exceeds browser cookie limits (~20 cookies or ~4KB per cookie), consider using stateful sessions or reducing session size by removing unnecessary custom claims.

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
Disadvantages:
  • Requires external data store
  • Additional latency for database queries
  • More complex infrastructure
Configuration:
Storage mechanism:
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.
How it works: With rolling sessions:
  • 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
Without rolling sessions (rolling: false):
  • Session expires after absoluteDuration regardless 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.
Or via environment variables:
Security Best Practices:
  • Always use secure: true in production (HTTPS)
  • Keep httpOnly: true (hardcoded by SDK) to prevent XSS attacks
  • Use sameSite: 'lax' or 'strict' for CSRF protection
  • Only set sameSite: 'none' if you need cross-site cookies (requires secure: true)

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:
Refresh flow:
Refresh Token Rotation: If your Auth0 application uses Refresh Token Rotation, configure an overlap period in your Auth0 Dashboard to prevent race conditions when multiple requests attempt to refresh tokens simultaneously.

Session Security

Encryption

All session data is encrypted using JWE (JSON Web Encryption) with the AUTH0_SECRET:

Session Lifecycle

Best Practices

  1. Use strong secrets: Generate with openssl rand -hex 32
  2. Enable HTTPS: Set secure: true in production
  3. Configure SameSite: Use 'lax' or 'strict' for CSRF protection
  4. Set reasonable durations: Balance security and user experience
  5. Monitor session size: Keep under 4KB for cookie-based sessions
  6. Implement logout: Clear sessions when users log out
  7. Use refresh tokens: Enable offline_access scope for long-lived sessions
  8. 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:
The session structure with MRRT:
Learn more about Multi-Resource Refresh Tokens in the documentation.

Session Debugging

To inspect session contents during development:

Next Steps