Cookies
Learn how cookies are used in Better Auth.
Cookies are used to store data such as session tokens, OAuth state, and more. All cookies are signed using the secret key provided in the auth options.
Cookie Prefix
Better Auth cookies will follow ${prefix}.${cookie_name} format by default. The prefix will be "better-auth" by default. You can change the prefix by setting cookiePrefix in the advanced object of the auth options.
Custom Cookies
All cookies are httpOnly and secure if the server is running in production mode.
If you want to set custom cookie names and attributes, you can do so by setting cookieOptions in the advanced object of the auth options.
By default, Better Auth uses the following cookies:
session_tokento store the session tokensession_datato store the session data if cookie cache is enableddont_rememberto store thedont_rememberflag if remember me 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.
Cross Subdomain Cookies
Sometimes you may need to share cookies across subdomains. For example, if you have app.example.com and example.com, and if you authenticate on example.com, you may want to access the same session on app.example.com.
By default, cookies are not shared between subdomains. However, if you need to access the same session across different subdomains, you can enable cross-subdomain cookies by configuring crossSubDomainCookies and defaultCookieAttributes in the advanced object of the auth options.
domain attribute broadens the cookie's scope beyond your main domain, making it accessible across all subdomains.sameSite cookie attribute to none makes you vulnerable to CSRF attacks. To mitigate risks, configure trustedOrigins with an array of authorized origins to allow.Secure Cookies
By default, cookies are secure only when the server is running in production mode. You can force cookies to be always secure by setting useSecureCookies to true in the advanced object in the auth options.