# Customer

An anonymous customer is created server-side when a visitor enters the site for the first time.<br>
The customer can later be authenticated by registering or signing in.

## Usage

```javascript
import { mockServer, CustomerApi } from 'geta-checkout';

const customerApi = new CustomerApi(mockServer.customerServices);

customerApi.get()
    .then(customer => console.log(`Hello ${customer.firstName}!`))
    .catch(console.error);
```

> **See [how to configure services](../readme.md)**

## Properties

| Name            | Required | Type                             | Description  |
|-----------------|----------|----------------------------------|--------------|
| firstName       | no       | String                           |              |
| lastName        | no       | String                           |              |
| isAuthenticated | no       | Boolean                          |              |
| email           | no       | String                           |              |
| phone           | no       | String                           |              |
| address         | no       | [Address](../address/readme.md)  |              |
| password        | no       | String                           | Only visible to the server, but can be updated from the frontend.

## Methods

### `get()`

→ `customer` or `Error`

```javascript
customerApi.get()
    .then(console.log)
    .catch(console.error);
```

### `update(properties)`

→ `customer` or `Error`

```javascript
customerApi.update({
        firstName: 'Alan',
        lastName: 'Turing',
        email: 'alan@loremips.um'
    })
    .then(console.log)
    .catch(console.error);
```

### `register(data)`

→ `customer` or `Error`

```javascript
customerApi.register(data)
    .then(console.log)
    .catch(console.error);
```

### `login(userId, password)`

→ `customer` or `Error`

```javascript
customerApi.login({
        userId: 'alan@loremips.um',
        password: 'secret'
    })
    .then(console.log)
    .catch(console.error);
```

### `logout()`

→ `customer` or `Error`

```javascript
customerApi.logout()
    .then(console.log)
    .catch(console.error);
```

### `resetPassword(userId)`

→ `customer` or `Error`

```javascript
customerApi.resetPassword('alan@loremips.um')
    .then(console.log)
    .catch(console.error);
```

### `search(query)`

→ `customer` or `Error`

```javascript
customerApi.search({phone: 12345678})
    .then(console.log)
    .catch(console.error);
```

## Tests

[Run unit tests](../../test/index.html?grep=Customer)