auth
Log customers in with a one-time code, register them, refresh their token and log them out.
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:
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 arriveLog in
// 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
| 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'.
await Salla.auth.register({
first_name: 'Sara',
last_name: 'Ahmed',
phone: '555555555',
country_code: 'SA',
verified_by: 'phone',
code: '1111',
});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
| 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
loginwith atypeother than'email'or'mobile'rejects at once and firesauth::code.not-sent.- A wrong code makes
verifyreject. Handle the promise:auth::verification.failedfires only when the SDK stops the call itself, or the response status is not 200. auth::verifiedfires whenever a code is accepted, also for a new customer who still has to register. Useauth::logged.into react to a login.- The guest's cart merges into the customer's cart on login. Read the cart again after
auth::logged.ininstead of keeping the old one.