Logging in#
Logging in, registering and resetting a password take the storefront token. They look the customer up within the channel's audience, so the same email address can have an account in two channels.
token is the user token; expires_at is when it expires, in unix milliseconds. A user token is valid for 24 hours. Store it in the af-sid cookie to share the login with the hosted checkout and account; see Sharing the login.
Send it as Authorization: Bearer <token> to read the account:
The customer's ids#
The user token is a JWT. Its payload holds the ids you need for addCustomerToCart:
| Claim | Value |
|---|---|
sub | The ID of the contact. |
organisation_id | The ID of the organisation, after logInAsOrganisation. Absent otherwise. |
channel | The ID of the channel. |
exp | When the token expires, in unix seconds. |
Decode the middle part of the token; you don't need to verify it, Afosto does that when you use it.
The account query doesn't return these ids. The ids of the customer's organisations are in account.organisations and account.shared_organisations.
Logging in doesn't change the cart. A cart only gets the customer, and the customer's prices, once you add the customer to it. See Loading the prices into the cart.
Related mutations#
| Mutation | Token | What it does |
|---|---|---|
registerCustomer | Storefront | Creates an account and returns a user token. Afosto sends a verification email. |
requestBusinessAccount | Storefront | Like registerCustomer, plus an organisation that can be used once it is approved in Afosto. |
requestCustomerVerificationLink | Storefront | Sends the verification email again. |
verifyCustomer | Storefront | Verifies the account with the token from the email and returns a user token. An unverified account can't log in. |
requestCustomerPasswordReset | Storefront | Sends a password reset email. |
resetCustomerPassword | Storefront | Sets a new password with the token from the email. |
setPasswordForAccount | User | Changes the password of the customer who is logged in. |
logInAsOrganisation | User | Returns a new user token that acts for one of the customer's organisations (account.organisations or shared_organisations). |
logOutAsOrganisation | User | Returns a user token for the personal account again. |
There is no logout mutation; see Logging out.
Verification and password emails#
registerCustomer, requestCustomerVerificationLink and requestCustomerPasswordReset make Afosto send an email from the channel. The channel needs a sender address, or no email is sent.
The links in the emails point to the channel's My account link, so by default the customer verifies and resets on the hosted account:
The emails are templates you can edit in Afosto. To handle the links in your storefront instead, change the link in the templates and, on your own page, pass the token parameter to verifyCustomer or resetCustomerPassword with the storefront token.
Errors#
Login errors come back in errors, with HTTP 200:
| Situation | Error |
|---|---|
| Unknown email address | could not find entity |
| Wrong password | The password you entered is incorrect |
| Account not verified yet | A message that the account has not yet been verified. Offer requestCustomerVerificationLink. |
Organisation not approved yet (logInAsOrganisation) | organisation is unapproved |
| Not a member of the organisation | contact cannot be authorized for organisation |
Expired or used link token (verifyCustomer, resetCustomerPassword) | The token has expired or was already used. Request a new email. |
| Expired user token, on any account call | unauthorized access. Log the customer in again. |