Customer Account Creation

Plan: Composable Developer

Lesson 22 of 27 · 15 min

Overview

A major remaining missing piece of any core ecommerce workflow is customer authentication. Allowing customer account registration and login is key to a friendly user experience, enabling customers to store important details like addresses and payment methods for repeated use, as well as to view and track order history. Customer accounts are also essential for features like price lists or various B2B functionality.

Registering Customers with GraphQL

The customer.registerCustomer mutation in the GraphQL Storefront API is used to create a new customer account.

Example Mutation and Response

This is a simple example of the mutation with its required input:

mutation RegisterCustomer($firstName: String!, $lastName: String!, $email: String!, $password: String!) {
customer {
registerCustomer(
input: {firstName: $firstName, lastName: $lastName, email: $email, password: $password}
) {
customer {
entityId
email
}
}
}
}

Additional details can be included when creating a customer. If it’s important in your storefront to initially collect address details and/or a phone number when a customer registers, you can include this information in the same request.

Example Request:

mutation RegisterCustomer(
...
) {
customer {
registerCustomer(
input: {
...
phone: "111-222-3333",
address: {
firstName: "John",
lastName: "Doe",
address1: "123 Park Central East",
address2: "Ste 1",
city: "Austin",
company: "My Store",
countryCode: "US",
stateOrProvince: "Texas",
phone: "444-555-6666",
postalCode: "78701"
}
}
) {
customer {
entityId
email
}
}
}
}

Associating Customers with the Correct Channel

All customer accounts are associated with a specific channel.

The origin_channel_id field on a customer record indicates the channel the account was created for, and the separate channel_ids field is a list of all channels the account is assigned to.

This is an example of a customer response from the REST Management API:

{
"data": [
{
"id": 45,
...
"origin_channel_id": 12345,
"channel_ids": [
1,
12345
]
}
],
...
}

Associating a customer account with the correct storefront channel via channel_ids is essential in order for GraphQL customer login to work successfully. If global logins are not enabled on your channel, this login mutation will fail if this field does not include the appropriate channel ID, even if it is set as origin_channel_id.

These fields must be manually populated when creating customer accounts via the REST Management API. When using the registerCustomer mutation in the GraphQL Storefront API, however, origin_channel_id and channel_ids will automatically be populated by the ID of the channel the request is made to.