Skip to main content
Commerce Engine distinguishes public requests from live user sessions. 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.

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:
For Next.js, TanStack Start, Astro, and SvelteKit, mount 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. Set register_if_not_exists: true to provide one login/register entry point.

1. Request an OTP

Phone and WhatsApp flows include the country code:
Persist both otp_token and otp_action in temporary form state. They belong to this verification attempt.

2. Verify the OTP

The SDK stores the new logged-in token pair when managed token storage is configured.

Passwordless login and registration

UX requirements

A production OTP flow should include:
Resend by requesting a new challenge and replacing the old 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
Refresh or invalidate the cart query after successful verification so the UI displays the merged state.

Password authentication

Password login is available for stores that enable it:
Password registration and reset can require OTP verification. Retrieve the exact operation contract from the LLM-first reference before building those flows rather than assuming one common request shape.

Reading authentication state

Use token helpers for fast UI state without an extra API call:
Use these values for presentation and route decisions. Server-side authorization must still rely on the request-bound session and the API operation result.

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

After a long absence, recovery should restore a usable anonymous session without blocking public browsing. Protected routes can then ask the customer to authenticate again.

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:
Auth profile methods may require an explicit user ID:
Do not accept an arbitrary browser-supplied user ID for a protected server operation when the SDK can infer the authenticated identity.

Profile updates

Changing a verified email or phone may require a dedicated OTP action. Confirm the exact operation and verification sequence from the current LLM reference.

Logout

Logout downgrades the logged-in session to a new anonymous session linked to the continuing journey.
With managed session storage, the SDK stores the returned anonymous tokens. Do not call 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
The anonymous cart and session identity can continue.

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
Do not treat every API error as proof the user is logged out.

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 ServerTokenStorage or server-side session storage
  • Node.js: request- or user-isolated storage
The Storefront API key is publishable. Private admin keys, payment credentials, and webhook secrets remain server-only. Never log access tokens, refresh tokens, OTPs, passwords, or full authentication responses.

Direct REST integration

Direct API clients must manage:
  • /auth/anonymous session creation
  • Bearer tokens
  • refresh and retry
  • response content envelopes
  • token replacement after login, refresh, and logout
Use the generated API reference for exact headers and operation contracts. SDK examples access unwrapped 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.