# Customer accounts

Source: https://snizu.dev/docs/storefronts/customer-accounts

## Ask the store first [#ask-the-store-first]

A store chooses whether shoppers have accounts and who may open one. Show sign-in and sign-up only where they work:

```ts title="account.ts"
const { accounts, sign_ups } = await shop.customer.accountSettings();
```

<Fields>
<Field name="accounts" type="off | optional | required">
`off`: guests only. `optional`: shoppers may sign in. `required`: checkout needs a signed-in customer.
</Field>

<Field name="sign_ups" type="open | approval | invited">
`open`: anyone can sign up. `approval`: a new account waits for the store. `invited`: no sign-up form; the store invites people.
</Field>
</Fields>

## A password [#a-password]

```ts title="sign-up.ts"
import { isAccountRequest } from "@snizu/storefront";

const result = await shop.customer.signUp({ email, password, first_name: "Salma", locale: "ar" });
if (isAccountRequest(result)) {
  showNotice("The store reviews new accounts. You'll get an email once yours is approved.");
}
const customer = await shop.customer.signIn(email, password);
```

A password is 12 to 128 characters. `requestPasswordReset(email)` emails a code, and `confirmPasswordReset` sets the new password with it and signs in.

## A code by email, SMS or WhatsApp [#a-code-by-email-sms-or-whatsapp]

Shoppers can sign in without a password. Ask where this store can send a code, then send it and check it:

```ts title="code.ts"
const channels = await shop.customer.signInCodeChannels(); // ["email", "sms", "whatsapp"]
await shop.customer.requestCode({ channel: "whatsapp", phone: "0100 123 4567" });
const result = await shop.customer.verifyCode({ phone: "0100 123 4567", code: "٤٨٢٩١٥" });
```

The phone can be written any usual way, read in the store's country, and the code's six digits can come with spaces, dashes or Arabic-Indic digits. When the code opens a new account, the answer has `created: true`; give `first_name`, `last_name`, `email` and `email_marketing` with the code to fill it in. Where the store approves new accounts, the answer is a pending request instead, as with a password.

## Refusals to expect [#refusals-to-expect]

<Fields>
<Field name="409 accounts_off">
The store has no customer accounts.
</Field>

<Field name="409 sign_up_invite_only">
The store opens accounts by invitation only.
</Field>

<Field name="409 account_pending">
The account still waits for the store's approval.
</Field>
</Fields>

## Signed in [#signed-in]

The session token lives where the cart's does, and every call about the customer carries it. Signing in links the cart to the customer, and `signOut()` forgets both.

<Fields>
<Field name="me(), update(changes), changePassword()">
The account, and changing it. `setBirthday("MM-DD")` too.
</Field>

<Field name="addresses(), addAddress(), updateAddress(), removeAddress()">
Their address book.
</Field>

<Field name="orders(), order(id), tracking(id), downloads(id)">
Their orders, newest first, where each parcel is, and the files and codes of a paid order.
</Field>

<Field name="buyAgain(orderId)">
Puts what's still for sale from a past order in the cart, at today's prices, and says what was left out and why. Start a cart first.
</Field>

<Field name="subscriptions(), changeSubscription(id, action)">
Skip the next renewal, pause, resume or cancel.
</Field>

<Field name="bookings(), cancelBooking(id), rescheduleBooking(id, …)">
Their bookings of the store's services, upcoming first. Cancelling one paid online refunds it, within the service's rules.
</Field>

<Field name="wishlist(), saveToWishlist(), removeFromWishlist()">
Saved products. `wishlistEnabled()` says whether the store offers wishlists; `shareWishlist()` gives a link, and `sharedWishlist(token)` reads one.
</Field>

<Field name="loyalty(), redeemPoints(points)">
Their points or cashback, tier and history, and turning points into store credit. `store.loyaltyProgram()` describes the programme to everyone.
</Field>

<Field name="fields(), setFields(values)">
The customer fields the store lets shoppers set themselves, such as a shoe size.
</Field>

<Field name="company()">
The company the customer buys for, or `null` for a retail customer.
</Field>
</Fields>

## Without an account [#without-an-account]

An order's email and text link to its status page with a token. `orderStatus.get(token)` reads that order for anyone holding the link, `confirm` and `cancel` act on an order that waits for the customer, and `find(number, contact)` gets the token from the order number and the email or phone it was placed with.