Configure how the SDK manages cookies for sessions and authentication transactions. The SDK uses two types of cookies: session cookies and transaction cookies.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/auth0/nextjs-auth0/llms.txt
Use this file to discover all available pages before exploring further.
Session Cookie Configuration
Session cookies store the encrypted user session. Configure session cookie attributes for security and functionality.Using Environment Variables
Set cookie options via environment variables in your.env.local file:
.env.local
The
httpOnly attribute is always set to true for security and cannot be configured.Using Auth0Client Options
Configure cookies programmatically when initializing the client:lib/auth0.ts
Options provided in
Auth0ClientOptions take precedence over environment variables.Session Cookie Options
| Option | Type | Default | Description |
|---|---|---|---|
domain | string | undefined | Cookie domain (e.g., .example.com for subdomains) |
path | string | / | URL path where cookie is valid |
transient | boolean | false | If true, cookie expires when browser closes |
secure | boolean | false | If true, cookie only sent over HTTPS |
sameSite | "lax" | "strict" | "none" | "lax" | CSRF protection level |
name | string | "__session" | Cookie name |
httpOnly | boolean | true | Always true (not configurable) |
Domain Configuration
Set the cookie domain to share sessions across subdomains:Path Configuration
Limit cookie scope to specific paths:Transient Sessions
Create session-only cookies that expire when the browser closes:- Shared/public computers
- Enhanced security requirements
- Temporary sessions
Secure Cookies
Require HTTPS for cookie transmission:When
appBaseUrl is omitted in production, the SDK automatically enforces secure: true. Attempting to set secure: false will throw an InvalidConfigurationError.SameSite Configuration
Control when cookies are sent with cross-origin requests:| Value | Behavior | Use Case |
|---|---|---|
"lax" | Sent on top-level navigation and same-site requests | Recommended - Balances security and usability |
"strict" | Only sent on same-site requests | High security, may break cross-origin flows |
"none" | Sent on all requests (requires secure: true) | Cross-origin scenarios, embedded apps |
Custom Cookie Name
Use a custom name for the session cookie:- Multiple Auth0 apps on same domain
- Avoiding name conflicts
- Custom naming conventions
Transaction Cookie Configuration
Transaction cookies maintain state during authentication flows (OAuth state parameter). Configure transaction cookie behavior to prevent issues with concurrent logins.Basic Configuration
lib/auth0.ts
Transaction Cookie Options
| Option | Type | Default | Description |
|---|---|---|---|
maxAge | number | 3600 (1 hour) | Expiration time in seconds |
prefix | string | "__txn_" | Cookie name prefix |
sameSite | "lax" | "strict" | "none" | "lax" | CSRF protection |
secure | boolean | Derived from appBaseUrl | Require HTTPS |
path | string | / | URL path |
Transaction Management Modes
The SDK supports two transaction management modes:Parallel Transactions (Default)
Allows multiple concurrent authentication flows:- Each login attempt gets a unique transaction cookie
- Cookie named:
__txn_{state} - Supports multiple tabs logging in simultaneously
- Cookies cleaned up after successful authentication
- Users might open multiple tabs and log in simultaneously
- You want maximum compatibility with typical user behavior
- Your application supports multiple concurrent authentication flows
Single Transaction Mode
Only one active transaction at a time:- Single transaction cookie:
__txn_ - New login attempts replace the previous transaction
- Simpler cookie management
- Prevents cookie accumulation
- You want to prevent cookie accumulation issues
- You prefer simpler transaction management
- Users typically don’t need multiple concurrent login flows
- You’re experiencing cookie header size limits
Parallel transactions are recommended for most applications. Use single transaction mode only if you’re experiencing cookie-related issues or have specific requirements.
Custom Transaction Cookie Prefix
Customize the transaction cookie name prefix:- Parallel mode:
auth_txn_{state} - Single mode:
auth_txn_
Transaction Cookie Expiration
Control how long transaction cookies remain valid:Dynamic Preview Environments
For dynamic preview environments (Vercel, Netlify), you can omitAPP_BASE_URL and let the SDK infer the base URL from the request:
lib/auth0.ts
- In production, the SDK enforces
secure: truefor cookies - Attempting to set
secure: falsethrowsInvalidConfigurationError - Protects against misconfiguration in production
When using dynamic base URLs, ensure all possible callback and logout URLs are registered in your Auth0 Application settings. Auth0’s Allowed URLs act as a security safeguard.
Complete Configuration Example
lib/auth0.ts
Best Practices
- Always use
secure: truein production to protect cookies from interception - Use
sameSite: "lax"for most applications (good balance of security and usability) - Set appropriate
domainonly when sharing sessions across subdomains you control - Keep
maxAgereasonable for transaction cookies (15-60 minutes) - Use parallel transactions unless you have specific reasons not to
- Test cookie settings in development to ensure they work as expected
- Monitor cookie size to stay within browser limits (4KB)
Security Considerations
Troubleshooting
Cookies not being set
If cookies aren’t being set:- Check that
secure: trueis only used with HTTPS - Verify
sameSite: "none"is paired withsecure: true - Ensure
domainis valid for your hostname - Check browser console for cookie warnings
Cookies not sent with requests
If cookies aren’t sent:- Verify
pathincludes the request path - Check
domainmatches the request hostname - Ensure
sameSitesettings allow the request type - Check for third-party cookie blocking in browser
Session not persisting
If sessions don’t persist:- Check if
transient: true(cookies expire on browser close) - Verify cookie expiration settings
- Ensure middleware is running on requests
- Check for cookie size limits (4KB max)
Transaction cookies accumulating
If you see many transaction cookies:- Switch to single transaction mode:
enableParallelTransactions: false - Reduce
maxAgeto clean up faster - Ensure authentication flows complete successfully
- Check that callback route is processing correctly