Skip to Content

Cookies

Learn how Entro Auth uses cookies, including cookie prefixes, custom cookie attributes, cross-subdomain sharing, secure cookies, and handling Safari ITP with proxies.

Cookies are used to store data such as session tokens, session data, OAuth state, and more.

  • The primary session_token cookie remains an opaque, secret-signed session identifier.
  • The session_data cookie is only present when session.cookieCache is enabled, and its encoding depends on the configured strategy:
    • compact: base64url + HMAC with your Entro Auth secret
    • jwt: HS256 by default, or asymmetric signing when sessionCookieCache is enabled on the JWT plugin
    • jwe: encrypted with your Entro Auth secret

If you use versioned secrets for rotation, secret-backed cookie data (including signed cookies and encrypted JWE session caches) will automatically use the current key and remain compatible with previous keys where supported.

By default, Entro Auth cookies follow the format ${prefix}.${cookie_name}. The default prefix is "entro-auth". You can change the prefix by setting cookiePrefix in the advanced object of the auth options.

auth.ts
import { entroAuth } from "@entro314labs/entro-auth"

export const auth = entroAuth({
    advanced: {
        cookiePrefix: "my-app"
    }
})

Custom Cookies

All cookies are httpOnly. When they get the Secure attribute is described in Secure Cookies.

If you want to set custom cookie names and attributes, you can do so by setting cookies in the advanced object of the auth options.

By default, Entro Auth uses the following cookies:

  • session_token to store the session token
  • session_data to store the session data if cookie cache is enabled
  • dont_remember to store the flag when rememberMe is disabled

Plugins may also use cookies to store data. For example, the Two Factor Authentication plugin uses the two_factor cookie to store the two-factor authentication state.

auth.ts
import { entroAuth } from "@entro314labs/entro-auth"

export const auth = entroAuth({
    advanced: {
        cookies: {
            session_token: {
                name: "custom_session_token",
                attributes: {
                    // Set custom cookie attributes
                }
            },
        }
    }
})

Cross Subdomain Cookies

Sometimes you may need to share cookies across subdomains. For example, if you authenticate on auth.example.com, you may also want to access the same session on app.example.com.

The domain attribute controls which domains can access the cookie. Setting it to your root domain (e.g. example.com) makes the cookie accessible across all subdomains. For security, follow these guidelines:

  1. Only enable cross-subdomain cookies if it's necessary
  2. Set the domain to the most specific scope needed (e.g. app.example.com instead of .example.com)
  3. Be cautious of untrusted subdomains that could potentially access these cookies
  4. Consider using separate domains for untrusted services (e.g. status.company.com vs app.company.com)
auth.ts
import { entroAuth } from "@entro314labs/entro-auth"

export const auth = entroAuth({
    advanced: {
        crossSubDomainCookies: {
            enabled: true,
            domain: "app.example.com", // your domain
        },
    },
    trustedOrigins: [
        'https://example.com',
        'https://app1.example.com',
        'https://app2.example.com',
    ],
})

Secure Cookies

Entro Auth decides whether cookies get the Secure attribute and the __Secure- name prefix in this order:

  1. If advanced.useSecureCookies is set, its value is used.
  2. Otherwise, with a dynamic baseURL config whose protocol is "https" or "http", cookies are secure for "https" only.
  3. Otherwise, when a base URL is known (from the baseURL option or an environment variable such as ENTRO_AUTH_URL), cookies are secure when it starts with https://.
  4. Otherwise (no base URL, or a dynamic config with protocol: "auto" or no protocol), cookies are secure when NODE_ENV is production.

To make cookies always secure, set useSecureCookies to true in the advanced object of the auth options.

auth.ts
import { entroAuth } from "@entro314labs/entro-auth"

export const auth = entroAuth({
    advanced: {
        useSecureCookies: true
    }
})

Safari, ITP, and Cross-Domain Setups

Safari includes a privacy feature called Intelligent Tracking Prevention (ITP) that blocks third-party cookies.

If your Entro Auth API is hosted on a different domain than your frontend, Safari may block authentication cookies entirely.

Example that breaks in Safari:

Frontend: https://app.domainB.com
API:      https://domainA.com

If your frontend makes a request like:

fetch("https://domainA.com/api/auth/get-session", {
  credentials: "include"
})

Safari treats domainA.com as a third-party and blocks its cookies. Which can result in sessions not persisting, Set-Cookie being ignored, users appearing logged out after login or auth working in Chrome but failing in Safari.

To solve this, there are two solutions:

  1. Using a reverse proxy to proxy the request to the API.
  2. Using a shared parent domain.

Using a Reverse Proxy

Instead of calling your API directly, you can proxy it through the same domain as your frontend.

For example, instead of calling:

https://domainA.com/api/auth/*

you can call:

https://app.domainB.com/api/auth/*

Then configure your hosting provider to proxy the request to your actual API server.

This makes the request appear first-party to Safari, allowing cookies to function correctly.

Example with Netlify

netlify.toml
[[redirects]]
  from = "/api/*"
  to = "https://domainA.com/api/:splat"
  status = 200
  force = true

Example with Vercel

vercel.json
{
  "rewrites": [
    {
      "source": "/api/:path*",
      "destination": "https://domainA.com/api/:path*"
    }
  ]
}

Using a Shared Parent Domain

You can also use a shared parent domain to allow cookies to be shared across subdomains:

https://app.example.com
https://api.example.com

Then, enable cross-subdomain cookies with the domain:

auth.ts
import { entroAuth } from "@entro314labs/entro-auth"

export const auth = entroAuth({
    advanced: {
        crossSubDomainCookies: {
            enabled: true,
            domain: "example.com",
        },
    },
})

Learn more about cross-subdomain cookies in the documentation above.

This avoids Safari treating the API as third-party.

On this page