# Customer Registration: Admin vs Client Form

This guide maps your **backend admin "Create Customer"** form to your **client-side registration form** and shows how to implement the flow so all client fields are stored.

## Implemented APIs and UI

- **Store (client):** `POST /store/register` — single endpoint for registration. Body: `full_name`, `email`, `phone`, `company_name`, `country`, `password`, `confirm_password`, `privacy_policy_accepted`. Creates auth identity + customer and stores all data in the database.
- **Admin:** `POST /admin/customers/register` — same payload; used when an admin creates a customer from the dashboard (requires admin auth).
- **Admin UI:** **Customers → Create Customer** — form with Full Name, Email, Phone, Company Name, Country, Password, Confirm Password, and Privacy policy checkbox. Uses the same fields and stores in the database.

## Field mapping

| Client form (registration)     | Medusa customer / auth        | Where it is stored                    |
|--------------------------------|-------------------------------|----------------------------------------|
| Full Name *                    | `first_name` + `last_name`    | Customer (split on first space or use first name only for last) |
| Email *                        | `email`                       | Customer + Auth identity               |
| Phone *                        | `phone`                       | Customer                              |
| Company Name                   | `company_name`                | Customer                              |
| Country *                      | `addresses[0].country_code`   | Customer address (ISO 2-letter, e.g. `us`) |
| Password *                     | —                             | Auth identity (not stored in customer) |
| Confirm Password *             | —                             | Client-only validation                 |
| Privacy policy checkbox        | `metadata.privacy_policy_accepted` | Customer `metadata`                |

**Admin dashboard** (Create Customer) only shows: First Name, Last Name, Email, Company, Phone. It does not show **Country** or **Privacy policy**; those are still saved when the client registers (see below). To see them in admin you would need to extend the admin customer UI to display `metadata` and default address country.

---

## How to implement client registration

Use Medusa’s standard **two-step** flow.

### Step 1: Get registration token (auth)

**Endpoint:** `POST /auth/customer/emailpass/register`  
**Body:**

```json
{
  "email": "user@example.com",
  "password": "their-secure-password"
}
```

- Validate on the client: required fields, password strength, password === confirm password, privacy checkbox checked.
- Send only `email` and `password` to this endpoint.
- On success you receive a **JWT** (in response or via cookie, depending on your Medusa/auth setup). Use it in the next request.

If the error says the identity already exists (e.g. email already registered), you can try **login** with the same email/password and then create the customer (see Medusa docs for “identity already exists” handling).

### Step 2: Create customer (with extra fields)

**Endpoint:** `POST /store/customers`  
**Headers:** `Authorization: Bearer <token>` (the token from step 1)  
**Body:**

```json
{
  "first_name": "John",
  "last_name": "Doe",
  "email": "user@example.com",
  "phone": "+1234567890",
  "company_name": "Acme Inc",
  "addresses": [
    {
      "country_code": "us"
    }
  ],
  "metadata": {
    "privacy_policy_accepted": true
  }
}
```

- **Full Name → first_name / last_name:** e.g. split `"John Doe"` into `first_name: "John"`, `last_name: "Doe"` (or put the whole string in `first_name` and leave `last_name` empty if you prefer).
- **Country:** use a 2-letter ISO code in `addresses[0].country_code` (e.g. `us`, `in`, `gb`). You can add more address fields later if needed.
- **Privacy policy:** store in `metadata.privacy_policy_accepted` (boolean or string; e.g. `true`).

After step 2, the customer exists with all the fields above. The admin “Create Customer” form will not show country or privacy in the default UI, but the data is in the customer record (address and metadata).

---

## Client-side checklist

1. **Validation (before calling APIs)**  
   - Required: Full Name, Email, Phone, Country, Password, Confirm Password.  
   - Passwords must match.  
   - Privacy policy checkbox must be checked.

2. **Call 1:** `POST /auth/customer/emailpass/register` with `email` and `password`.  
   - Store or use the returned JWT for the next request.

3. **Call 2:** `POST /store/customers` with `Authorization: Bearer <token>` and body with:
   - `first_name`, `last_name` (from Full Name),
   - `email`, `phone`, `company_name`,
   - `addresses: [{ country_code: "<country>" }]`,
   - `metadata: { privacy_policy_accepted: true }`.

4. **After success:** redirect to login or auto-login using the same token (see Medusa storefront auth docs).

---

## Admin dashboard

- **Create Customer** in the admin uses the default Medusa fields only (first name, last name, email, company, phone).
- Data that the **client** sent (country in address, privacy in metadata) is stored and can be read via:
  - Admin API / customer module (if you add a custom admin view), or
  - Exposing customer `metadata` and default address in your admin when you extend the customer UI.

No backend code change is required for the standard two-step registration; only the client must call both endpoints and send the payload above so that country and privacy are stored on the customer.
