# auth (https://docs.salla.dev/twilight/react/sdk/auth)



Use `Salla.auth` when you build your own login flow. Most themes do not: the engine's login popup (`salla-login-modal`) runs the whole flow,
and you open it with `Salla.event.dispatch('login::open')`.

Customers log in without a password. They enter an email or a mobile number, receive a code, and enter the code:

```text
login({ type, email | phone })   the store sends a code
verify({ type, code, ... })      the customer is logged in
resend({ type, ... })            a new code, if the first one did not arrive
```

## Log in [#log-in]

```ts
// 1. send the code
await Salla.auth.login({ type: 'mobile', phone: '555555555', country_code: 'SA' });
// or: await Salla.auth.login({ type: 'email', email: 'customer@example.com' });

// 2. check the code the customer typed
await Salla.auth.verify({ type: 'mobile', phone: '555555555', country_code: 'SA', code: '1111' });
```

After `verify` succeeds, the SDK saves the token, merges the guest's cart into the customer's cart, loads the profile and fires `auth::logged.in`.

## Methods [#methods]

| Method     | Parameters                                                                  | Does                                                          |
| ---------- | --------------------------------------------------------------------------- | ------------------------------------------------------------- |
| `login`    | `{ type: 'email', email }` or `{ type: 'mobile', phone, country_code }`     | Sends a login code                                            |
| `verify`   | the same fields plus `code`                                                 | Checks the code and logs the customer in                      |
| `resend`   | the same fields as `login`                                                  | Sends a new code. `type` defaults to `'mobile'`.              |
| `register` | `{ first_name, last_name, email?, phone, country_code, verified_by, code }` | Creates the customer's account                                |
| `refresh`  | none                                                                        | Gets a new token for a customer who is logged in on the store |
| `logout`   | none                                                                        | Logs the customer out and reloads the store                   |

`verified_by` is where the code was sent: `'phone'` or `'email'`.

```ts
await Salla.auth.register({
  first_name: 'Sara',
  last_name: 'Ahmed',
  phone: '555555555',
  country_code: 'SA',
  verified_by: 'phone',
  code: '1111',
});
```

## Log out [#log-out]

`logout` clears what the SDK saved in the browser (cart, wishlist, token, profile), fires `auth::logged.out`,
logs the customer out of their Salla account, then loads the store's logout page.
Do any clean-up in an `auth::logged.out` listener: the page reloads right after it.

## Events [#events]

| Event                                            | Fires when                                              | Arguments                                   |
| ------------------------------------------------ | ------------------------------------------------------- | ------------------------------------------- |
| `auth::code.sent` / `auth::code.not-sent`        | `login`, `resend`                                       | response or error, then the type or payload |
| `auth::verified` / `auth::verification.failed`   | `verify`                                                | response or error, type                     |
| `auth::logged.in`                                | The customer is logged in, after `verify` or `register` | response                                    |
| `auth::registered` / `auth::registration.failed` | `register`                                              | response or error                           |
| `auth::token.fetched`                            | A new token was saved                                   | token                                       |
| `auth::refresh.failed`                           | `refresh` failed                                        | error                                       |
| `auth::logged.out`                               | `logout`, before the page leaves                        | none                                        |

## Gotchas [#gotchas]

* `login` with a `type` other than `'email'` or `'mobile'` rejects at once and fires `auth::code.not-sent`.
* A wrong code makes `verify` reject. Handle the promise: `auth::verification.failed` fires only when the SDK stops the call itself, or the response status is not 200.
* `auth::verified` fires whenever a code is accepted, also for a new customer who still has to register. Use `auth::logged.in` to react to a login.
* The guest's cart merges into the customer's cart on login. Read the cart again after `auth::logged.in` instead of keeping the old one.

## See also [#see-also]

* [Profile](https://docs.salla.dev/twilight/react/sdk/profile)
* [`useUser`](https://docs.salla.dev/twilight/react/hooks/use-user)
* [Web components: `salla-login-modal`](https://docs.salla.dev/twilight/react/web-components/salla-login-modal)
* [Events](https://docs.salla.dev/twilight/react/sdk/events)
