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.

mutation LogIn($input: LogInCustomerInput!) {
  logInCustomer(input: $input) {
    token
    expires_at
  }
}
{
  "input": {
    "email": "jane@example.com",
    "password": "correct horse battery staple"
  }
}

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:

query Account {
  account {
    email
    given_name
    family_name
    organisations {
      id
      name
    }
  }
}

The customer's ids#

The user token is a JWT. Its payload holds the ids you need for addCustomerToCart:

ClaimValue
subThe ID of the contact.
organisation_idThe ID of the organisation, after logInAsOrganisation. Absent otherwise.
channelThe ID of the channel.
expWhen 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.

function claims(token) {
  const payload = token.split('.')[1].replace(/-/g, '+').replace(/_/g, '/');
  return JSON.parse(atob(payload));
}

const { sub: contactId, organisation_id: organisationId } = claims(token);

The account query doesn't return these ids. The ids of the customer's organisations are in account.organisations and account.shared_organisations.

⚠
Warning:

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.

MutationTokenWhat it does
registerCustomerStorefrontCreates an account and returns a user token. Afosto sends a verification email.
requestBusinessAccountStorefrontLike registerCustomer, plus an organisation that can be used once it is approved in Afosto.
requestCustomerVerificationLinkStorefrontSends the verification email again.
verifyCustomerStorefrontVerifies the account with the token from the email and returns a user token. An unverified account can't log in.
requestCustomerPasswordResetStorefrontSends a password reset email.
resetCustomerPasswordStorefrontSets a new password with the token from the email.
setPasswordForAccountUserChanges the password of the customer who is logged in.
logInAsOrganisationUserReturns a new user token that acts for one of the customer's organisations (account.organisations or shared_organisations).
logOutAsOrganisationUserReturns 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:

https://account.shop.com/verify?token=…
https://account.shop.com/reset-password?token=…

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:

SituationError
Unknown email addresscould not find entity
Wrong passwordThe password you entered is incorrect
Account not verified yetA message that the account has not yet been verified. Offer requestCustomerVerificationLink.
Organisation not approved yet (logInAsOrganisation)organisation is unapproved
Not a member of the organisationcontact 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 callunauthorized access. Log the customer in again.
Query Runnerhttps://afosto.app/graphql

No query loaded

Click play on any code block in the docs to load a query here.