- Get Started
- Product
- Resources
- Tools & SDKs
- Framework
- Reference
Menu
- Get Started
- Product
- Resources
- Tools & SDKs
- Framework
- Reference
Register Customer in Storefront
To register a customer, you implement the following steps:
- Show the customer a form to enter their details.
- Send a
POST
request to the/auth/customer/emailpass/register
API route to obtain a JWT token. - Send a request to the Create Customer API route pass the JWT token in the header.
However, a customer may enter an email that's already used either by an admin user, another customer, or a custom actor type. To handle this scenario:
- Try to obtain a login token by sending a
POST
request to the/auth/customer/emailpass
API route. The customer is only allowed to register if their email and password match the existing identity. This allows admin users to log in or register as customers. - If you obtained the login token successfully, create the customer using the login JWT token instead of the registration token. This will not remove the existing identity. So, for example, an admin user can also become a customer.
An example implemetation of the registration flow in a storefront:
In the above example, you create a handleRegistration
function that:
- Obtains a JWT token from the
/auth/customer/emailpass/register
API route. - If an error is returned instead of a token:
- If the error is an existing identity error, try retrieving the login JWT token from
/auth/customer/emailpass
API route. This will fail if the existing identity has a different password, which doesn't allow the customer from registering. - For other errors, show an alert and exit execution.
- If the error is an existing identity error, try retrieving the login JWT token from
- Send a request to the Create Customer API route, and pass the registration or login JWT token as a Bearer token in the authorization header.
- If an error occurs, show an alert and exit execution.
- Once the customer is registered successfully, you can either redirect the customer to the login page or log them in automatically as explained in this guide.
Was this page helpful?