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 arrive

Log 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

MethodParametersDoes
login{ type: 'email', email } or { type: 'mobile', phone, country_code }Sends a login code
verifythe same fields plus codeChecks the code and logs the customer in
resendthe same fields as loginSends a new code. type defaults to 'mobile'.
register{ first_name, last_name, email?, phone, country_code, verified_by, code }Creates the customer's account
refreshnoneGets a new token for a customer who is logged in on the store
logoutnoneLogs 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

EventFires whenArguments
auth::code.sent / auth::code.not-sentlogin, resendresponse or error, then the type or payload
auth::verified / auth::verification.failedverifyresponse or error, type
auth::logged.inThe customer is logged in, after verify or registerresponse
auth::registered / auth::registration.failedregisterresponse or error
auth::token.fetchedA new token was savedtoken
auth::refresh.failedrefresh failederror
auth::logged.outlogout, before the page leavesnone

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

On this page