Auth
Signing a customer into the store with a one-time email code Two calls sign in: request emails a 6-digit code, verify checks it and signs the customer into the store. Two more keep the session: session says who is signed in, sign-out ends it.
The session (one rule)
verify signs the customer into this store. The platform sets the
session in its answer as a cookie on the address the storefront called:
| Cookie | Readable by the page | What it is |
|---|---|---|
_cartbase_session |
No (HttpOnly) | The session. 30 days, renewed by session at most once a day, ended by sign-out. Stored by the platform only as its hash, bound to its store. |
_cartbase_signed_in |
Yes | 1 while a customer is signed in. No secret: it tells the page to show the account's door and to ask session who it is. |
Both are Secure, SameSite=Lax, Path=/. The answer's body carries the
customer and no token, so no script on the page ever holds the credential,
the pixels and chat widgets included.
The rule that follows: the browser calls the store API through the
storefront's own address. A Cartbase storefront proxies /api/store/*
to the platform (the scaffold's next.config.ts rewrite), so the cookie is
filed under the store's host and the browser sends it back on every call:
customers/me, orders, documents, addresses, cart attach. A browser client
built on the platform's address directly never signs in.
Nothing is persisted by the storefront and nothing is refreshed. A customer
bearer token from the platform's password sign-in (passed as
getAuthToken) is still accepted by every customer route.
What the login decides
A signed-in customer is served as themselves: their prices (their groups'
price lists) and, on a key whose sales channels are split by the B2B flag,
their catalogue. A customer in the store's B2B group (customer.b2b) reads
the key's B2B channel and only it; everyone else reads the rest. Their cart
follows: a B2B customer's new cart is born on the B2B channel, and a cart
they sign into moves there and is repriced. A signed-in answer is never
held by a shared cache, and the SDK leaves the key out of a signed-in
shopper's catalogue address so no held answer can reach them.
Server-enforced security posture (the SDK adds nothing)
- Request never reveals whether the email is registered:
{ok: true}always. The code exists only in the sent email. - Codes: hashed at rest, live 10 minutes, single-use, one live code per email (a new request supersedes the earlier ones), locked after 5 wrong attempts.
- Rate limits per 15 minutes: 5 requests per email, 20 per address,
answered
429 rate_limited, the only failure a request can show. - Every verify failure (wrong code, expired, used, locked, unknown email) is
the same
401 invalid_code. - A store whose customer accounts are Disabled (Settings, Customer
accounts) refuses request and verify with
403 accounts_disabled;GET /api/store/storecarries the mode asstore.accounts.mode.
POST /api/store/auth/code/request: step 1
- Purpose: email a 6-digit one-time code. A first-time email registers at VERIFY, not here.
- Auth: the store's key.
- Request:
{email: string}. - Response:
{ok: true}, always, by design. - Errors:
429 rate_limited·403 accounts_disabled·400 validation_failed(malformed email) ·400 missing_client_id. - SDK:
auth.requestLoginCode(client, {email}) - Components: the sign-in form (account pages family).
- Settings: the
auth-codenotification template carries the code; Settings, Customer accounts.
# The request contract: {ok:true}, nothing else. No code echo, no
# registered-or-not oracle. RUN-stamped email, so no rate-limit collisions.
curl -sf -X POST "$BASE/api/store/auth/code/request" \
-H "x-client-id: $CLIENT_ID" -H "content-type: application/json" \
-d '{"email": "doc-auth-'$RUN'@doc.test"}' \
| grep -q '^{"ok":true}$'POST /api/store/auth/code/verify: step 2
- Purpose: check (email, code) and sign the customer into the store.
This is also registration: a first-time email creates the customer
(
account_statusper store policy), a guest who ordered with the email before is upgraded in place (their orders become the account's), and the welcome email goes out. - Auth: the store's key.
- Request:
{email: string, code: string}; the code is exactly 6 digits (leading zeros count; any other shape is400 validation_failed). - Response:
{ customer }, the full customer with addresses (see customers.md), and the two cookies above inSet-Cookie. - Errors:
401 invalid_code(every failure, the same) ·403 accounts_disabled·400 validation_failed·400 missing_client_id. - SDK:
auth.verifyLoginCode(client, {email, code}) - Components: the code-entry form (account pages family).
- Settings: store approval policy (new-customer
account_status);account-welcometemplate.
# The 401 invalid_code contract: a well-formed but wrong code against the
# RUN-stamped email (its real code lives only in the email, which is never read here).
STATUS=$(curl -s -o /dev/null -w '%{http_code}' \
-X POST "$BASE/api/store/auth/code/verify" \
-H "x-client-id: $CLIENT_ID" -H "content-type: application/json" \
-d '{"email": "doc-auth-'$RUN'@doc.test", "code": "000000"}')
test "$STATUS" = 401
curl -s -X POST "$BASE/api/store/auth/code/verify" \
-H "x-client-id: $CLIENT_ID" -H "content-type: application/json" \
-d '{"email": "doc-auth-'$RUN'@doc.test", "code": "000000"}' \
| grep -q '"code":"invalid_code"'GET /api/store/auth/session: who is signed in
- Purpose: the customer the session belongs to, or nobody. Ask it once
per page load when
_cartbase_signed_insays there is a session; a visitor who never signed in costs no request. - Auth: the session cookie. A bearer token names no session here.
- Response: always
200.{ customer }for a live session,{ customer: null }otherwise. A session that has ended has both cookies cleared in the same answer; a live one is renewed for another 30 days, at most once a day, with the cookies set again. - SDK:
auth.getSession(client) - Components: the session provider (account pages family).
# Nobody signed in: always 200, a null customer.
curl -sf "$BASE/api/store/auth/session" -H "x-client-id: $CLIENT_ID" \
| grep -q '^{"customer":null}$'
# A cookie that names no session: nobody, and both cookies cleared.
HEADERS=$(curl -sfD - -o /dev/null "$BASE/api/store/auth/session" \
-H "x-client-id: $CLIENT_ID" \
-H "cookie: _cartbase_session=not-a-session; _cartbase_signed_in=1")
echo "$HEADERS" | grep -qi '^set-cookie: _cartbase_session=;.*max-age=0'
echo "$HEADERS" | grep -qi '^set-cookie: _cartbase_signed_in=;.*max-age=0'POST /api/store/auth/sign-out: end the session
- Purpose: revoke the session on this store, so a copy of the cookie
opens nothing, and clear both cookies. The storefront forgets its cart
with it (
clearCartCookie): the cart belonged to the customer who left. - Auth: the session cookie; nobody signed in is the same answer.
- Response:
{ok: true}. - SDK:
auth.signOut(client) - Components: the account page's Sign out.
curl -sf -X POST "$BASE/api/store/auth/sign-out" -H "x-client-id: $CLIENT_ID" \
| grep -q '^{"ok":true}$'Cleanup note: the code rows this doc creates are single-per-run (RUN-stamped email), superseded on any later request and dead after the 10-minute TTL; there is no store-surface delete for them by design (they are the auth trail). The signed-in path (the cookies, the session row kept as its hash, renewal, sign-out, the store binding, the B2B catalogue) needs a code from a real mailbox, so it is shown as shapes here and not run.