Public browsing does not require anonymous authentication. Establish a live session only when the application needs session-bound behaviour or when it intentionally bootstraps one at startup.
Recommended implementation paths
Hosted Checkout
Recommended when login is primarily needed for checkout. Hosted Checkout handles OTP, account switching, addresses, payment, and order confirmation.
Custom auth UI
Build this when the storefront needs login state outside checkout—for example, account, orders, loyalty, wishlist, or saved addresses.
Session bootstrap
For a browser SPA:storefront.bootstrap() once in a root client boundary. Returning users reuse existing cookie-backed sessions.
Do not create anonymous sessions during SSG, ISR, prerendering, or public server rendering. Use the public accessor in those contexts.
Anonymous session bootstrap
Passwordless OTP login
Commerce Engine supports email, phone, and WhatsApp OTP flows. Setregister_if_not_exists: true to provide one login/register entry point.
1. Request an OTP
otp_token and otp_action in temporary form state. They belong to this verification attempt.
2. Verify the OTP
Passwordless login and registration
UX requirements
A production OTP flow should include:otp_token and otp_action.
Anonymous cart continuity
When an anonymous user logs in, Commerce Engine merges the active anonymous cart into the account session. Do not:- create a new cart after login
- copy cart items client-side
- discard the returned token pair
Password authentication
Password login is available for stores that enable it:Reading authentication state
Use token helpers for fast UI state without an extra API call:Automatic refresh and session recovery
With managed storage, the SDK refreshes expired access tokens, persists rotated tokens, and retries the original operation once. Concurrent requests should share one refresh rather than launching several refresh calls.Automatic token refresh
Session recovery after a long absence
User versus customer ID
For many B2C stores, user and customer IDs align. B2B accounts can contain multiple users under one customer. Prefer SDK overloads that resolve identity from the active session:Profile updates
Logout
Logout downgrades the logged-in session to a new anonymous session linked to the continuing journey.clearTokens() after a successful logout.
After logout:
- remove protected account data from local caches
- invalidate cart/session queries
- update header UI from the new anonymous user state
- redirect away from protected routes
Logout with anonymous continuity
Hosted Checkout authentication
Checkout-only integration
When Hosted Checkout is the only Commerce Engine integration, use its default managed mode. Checkout owns anonymous bootstrap, OTP login, refresh, and logout.Storefront SDK plus Hosted Checkout
When the app also uses@commercengine/storefront, the Storefront SDK is the session owner. Use authMode: "provided" and two-way token synchronization.
This requirement applies even when the app has no custom auth form. Otherwise the SDK and checkout create separate users/carts.
See Hosted Checkout configuration.
Route protection
Protect account routes using the framework’s request-bound session accessor where possible. A useful guard distinguishes:- no session / anonymous → redirect to login
- logged-in session → render
- upstream failure → error/retry state, not automatic login redirect
Token storage and security
Use the storage model designed for the runtime:- browser SPA:
BrowserTokenStorage - first-party SSR wrapper: built-in aligned cookie storage
- custom SSR/BFF: request-bound
ServerTokenStorageor server-side session storage - Node.js: request- or user-isolated storage
Direct REST integration
Direct API clients must manage:/auth/anonymoussession creation- Bearer tokens
- refresh and retry
- response
contentenvelopes - token replacement after login, refresh, and logout
data and must not be copied into a direct REST client unchanged.
Production checklist
Exact auth operations
Retrieve current auth method signatures and types with a
.md URL or Accept: text/markdown before implementation.