Skip to main content
Configure how the SDK manages cookies for sessions and authentication transactions. The SDK uses two types of cookies: session cookies and transaction cookies. 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 SDK automatically picks up these values at runtime.
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.

Domain Configuration

Set the cookie domain to share sessions across subdomains:
Setting domain to a parent domain allows all subdomains to access the cookie. Only use this when you control all subdomains.

Path Configuration

Limit cookie scope to specific paths:

Transient Sessions

Create session-only cookies that expire when the browser closes:
Use cases:
  • 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:
Options:
Using sameSite: "none" requires secure: true and HTTPS. Browsers reject insecure cookies with SameSite=None.
Use a custom name for the session cookie:
Use cases:
  • Multiple Auth0 apps on same domain
  • Avoiding name conflicts
  • Custom naming conventions
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 Management Modes

The SDK supports two transaction management modes:

Parallel Transactions (Default)

Allows multiple concurrent authentication flows:
How it works:
  • Each login attempt gets a unique transaction cookie
  • Cookie named: __txn_{state}
  • Supports multiple tabs logging in simultaneously
  • Cookies cleaned up after successful authentication
Use when:
  • 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:
How it works:
  • Single transaction cookie: __txn_
  • New login attempts replace the previous transaction
  • Simpler cookie management
  • Prevents cookie accumulation
Use when:
  • 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.
Customize the transaction cookie name prefix:
Result:
  • Parallel mode: auth_txn_{state}
  • Single mode: auth_txn_
Control how long transaction cookies remain valid:
Setting maxAge too low may cause authentication to fail if users take too long to complete the Auth0 login form.

Dynamic Preview Environments

For dynamic preview environments (Vercel, Netlify), you can omit APP_BASE_URL and let the SDK infer the base URL from the request:
lib/auth0.ts
Automatic enforcement:
  • In production, the SDK enforces secure: true for cookies
  • Attempting to set secure: false throws InvalidConfigurationError
  • 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: true in production to protect cookies from interception
  • Use sameSite: "lax" for most applications (good balance of security and usability)
  • Set appropriate domain only when sharing sessions across subdomains you control
  • Keep maxAge reasonable 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

  • Never set httpOnly: false - This is not allowed by the SDK for security reasons
  • Use secure: true in production - Prevents cookie theft over insecure connections
  • Be cautious with domain settings - Overly broad domains expose cookies to more subdomains
  • Use sameSite protection - Prevents CSRF attacks
  • Validate dynamic base URLs - Ensure Auth0 Allowed URLs are properly configured

Troubleshooting

Cookies not being set

If cookies aren’t being set:
  • Check that secure: true is only used with HTTPS
  • Verify sameSite: "none" is paired with secure: true
  • Ensure domain is valid for your hostname
  • Check browser console for cookie warnings

Cookies not sent with requests

If cookies aren’t sent:
  • Verify path includes the request path
  • Check domain matches the request hostname
  • Ensure sameSite settings 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 maxAge to clean up faster
  • Ensure authentication flows complete successfully
  • Check that callback route is processing correctly