Overview
The User Session is automatically saved in two cookies:stytch_session
Contains the opaque
session_token returned from the API.stytch_session_jwt
Contains the
session_jwt returned from the API.https://app.example.com, the cookies will be set as:
document.cookie = "stytch_session=secret-session; domain=app.example.com; path=/; SameSite=Lax; Secure; max-age=whenever-it-expires"document.cookie = "stytch_session_jwt=eyJhxxx.xxx.xxx; domain=app.example.com; path=/; SameSite=Lax; Secure; max-age=whenever-it-expires"
The cookie will not be marked as secure if the SDK is loaded on
localhost.Configure cookie options
The Stytch SDK allows you to configure the following directives and values for cookie management viacookieOptions:
cookieOptions do not apply to HttpOnly cookies.HttpOnly cookies
For added security, you can enable the use of HttpOnly cookies for your project. This moves cookie management from the client to Stytch’s backend. When HttpOnly cookies are used, Stytch’s backend sets theDomain of the cookie to the parent of your custom domain. Cookies are accessible to the Domain specified and all its subdomains, for example:
-
If your custom domain is
login.example.com, theDomainwill be set toexample.com, making the cookies accessible toexample.com,app.example.com,login.example.com, etc. -
If your custom domain is
login.app.example.com, theDomainwill be set toapp.example.com, making the cookies accessible toapp.example.comand its subdomains, but notexample.com.
By default, cookies managed by the SDK running in the client are not marked as
HttpOnly.Enable HttpOnly cookies
Important things to consider before enabling HttpOnly cookies:Requirements
- Using HttpOnly cookies requires a custom domain to be set up for your Stytch project to allow Stytch’s backend to set cookies in a first-party context for your domain.
- HttpOnly cookies are only supported in the live environment.
Considerations
-
Using HttpOnly cookies means the cookies are no longer accessible via JavaScript in the browser. This includes the
session_tokenandsession_jwt. These tokens are omitted from any response bodies to the client. -
session.getTokens()will no longer return session tokens when an active session exists. -
session.updateSession()will only work if there’s not an existing session token set using an HttpOnly cookie. Make surecookieOptionsare configured to match the domain and cookie names used by Stytch’s backend if you use this method. -
If the HttpOnly cookies setting is set to
enforced, Stytch will only accept browser requests for your project that use your custom domain. -
cookieOptionsdoes not apply to HttpOnly cookies.
Set a custom domain
Configure a custom domain for your project.
Enable HttpOnly cookies
Set the HttpOnly cookies setting to
Enabled under the Frontend SDK settings.Update your SDK client
In your application, set the
customBaseUrl option in your Stytch SDK client to your custom domain to tell the SDK to use your custom domain for requests.Test your implementation
Verify that your application behaves as expected. The
stytch_session and stytch_session_jwt cookies should now be marked as HttpOnly.