Public catalog browsing does not require an anonymous session. Create a live session only when the customer begins a session-bound flow or when the application intentionally bootstraps one at startup.
Browser SPA
UseBrowserTokenStorage inside the session configuration of createStorefront().
lib/storefront.ts
ensureAccessToken() before every cart, customer, or order call. Managed session methods can ensure or refresh the session internally.
First-party SSR frameworks
Next.js, TanStack Start, Astro, and SvelteKit wrappers use matching browser and request-cookie storage. The shared model is:publicStorefront()never reads or writes session tokensclientStorefront()uses the browser-side cookie-backed sessionserverStorefront()uses the current request’s cookiesbootstrap()creates the first anonymous browser session when no cookies exist- returning users hydrate from existing cookies on the first request
First-party wrappers use cookies that the browser SDK can read and write, so they are not HttpOnly. This is intentional for a client-managed storefront session. If your security architecture requires HttpOnly tokens, place Commerce Engine behind a backend-for-frontend and own the token lifecycle there instead of using the browser session client.
First visit
On the first visit there are no session cookies. Mount the framework bootstrap once near the root:Returning visit
Existing cookies are available to request-bound server accessors immediately. A returning user’s account or cart can therefore render on the first request where the framework supports request-time rendering.Peek versus ensure
The session helper exposes passive and active reads.Peek
Peek methods read current state without creating a session or refreshing tokens. They returnnull when unavailable.
Ensure
Ensure methods may create an anonymous session or refresh an expired token.Authentication transition
OTP or password login returns a new, more privileged token pair. With managed session storage, the SDK stores those tokens and invokes the token-update callback.Logout continuity
Logout does not end the Commerce Engine session completely. It replaces the logged-in tokens with a new anonymous token pair tied to the continuing user journey.clearTokens() after a successful logout, because that discards the new anonymous session and can break cart and analytics continuity.
Update the UI from the resulting user/token state:
clearTokens() only for an unrecoverable local session, explicit account removal, or a security response where continuity should be discarded.
Refresh and recovery
Managed session mode refreshes access tokens when required. Application code should normally respond to the final{ data, error } result rather than implementing a second refresh loop around every call.
When both access and refresh tokens are invalid, a session-bound method may need to establish a new anonymous session. Treat the resulting state as anonymous and require login again for protected account operations.
Do not log token values or expose them through error monitoring.
Parameterless session overloads
Many session-scoped SDK methods can resolveuser_id or customer_id from the active token.
Hosted Checkout session synchronization
When@commercengine/storefront and @commercengine/checkout are both present, there must be one session owner. The Storefront SDK owns the session and Hosted Checkout runs in provided mode.
Two-way synchronization is required because either side may refresh tokens or complete login/logout.
SDK to checkout
For a browser SPA, configure the callback insidesession:
onTokensUpdated option as shown in its framework guide.
Checkout to SDK
Server-side isolation
Never share one mutable session SDK withMemoryTokenStorage across concurrent end users. Create a request-bound SDK or storage adapter so one user’s token cannot be observed by another request.
Public SDK instances, which carry no user session, can generally be shared.
See Node.js for server patterns.
Security checklist
Framework session guides
Use the framework integration pages for exact bootstrap and request-cookie patterns.