app/sessions.ts) configures and exports React Router’s cookie session storage. It stores the Stripe customerId after registration so that server-side loaders and actions can identify the current user without a database lookup.
Overview
The module callscreateCookieSessionStorage from react-router to create an encrypted, signed cookie named __session. The cookie carries two types of data:
- Session data — persisted across requests:
customerId - Flash data — available only for the next request, then cleared:
error
getSession, commitSession, and destroySession — are re-exported for use throughout the app.
Cookie configuration
The session cookie is configured with the following options:The name of the cookie set in the browser. Firebase Hosting and some CDNs strip cookies other than
__session, so this name is chosen for broad compatibility.Prevents client-side JavaScript from accessing the cookie. Mitigates XSS-based session theft.
Cookie lifetime in seconds. The current value of
60 seconds is for development. Set this to a longer duration (e.g. 604800 for 7 days) in production.Restricts the cookie to this URL path prefix.
/ makes it available on all routes.Controls cross-site request behavior.
lax allows the cookie to be sent on top-level navigations, protecting against CSRF while keeping normal link navigation working.An array of secrets used to sign and verify the cookie. React Router uses the first secret to sign new cookies and accepts all secrets for verification, enabling zero-downtime secret rotation. Replace
["s3cret1"] with a cryptographically random value.Restricts the cookie to HTTPS connections. Set to
false only in local development if you are not using HTTPS.Session data shape
customerId holds the Stripe customer ID (e.g. cus_Abc123) returned by createCustomer at registration. Every route that needs to call a Stripe API reads this value from the session.
Flash data shape
error key is used to surface server-side error messages — for example, when a payment fails — across a redirect boundary.
Exported functions
All three functions are created bycreateCookieSessionStorage and re-exported directly:
Reads and deserializes the session from the
Cookie request header. Returns a Session object. Always call this at the top of a loader or action that needs session data.Serializes the session back into a signed cookie string. Use the return value as the
Set-Cookie response header to persist any changes (including new flash messages) to the client.Invalidates the session cookie by setting it to expire immediately. Call this in a logout action to clear the customer’s session.
Usage pattern
The following examples show how to read and write session data in a React Router loader and action. Reading the session in a loaderaccount loader
register action
error flash
logout action
Always pass the raw
Cookie header string — request.headers.get("Cookie") — to getSession. Passing null is safe and returns an empty session, which is useful when no cookie is present on the first visit.