New Customer Account API Migration
Follow these steps to migrate from the legacy customer accounts to the new Customer Account API.
This migration is required before integrating the Shopify B2B logic into the codebase.
Migration Caveats:
- The logged in experience will no longer be supported while in the Pack Customizer due to the nature of customer authentication
- Customer accounts doesn't support the use of
localhost
due to security concerns. In order to use customer accounts while in development, localhost must be hosted on a public URL via ngrok (see below for instructions) - Multipass is currently not supported, though if a customer is logged in and goes to checkout, logging in will be a one click action
Local Development and Hydrogen Settings
For official Shopify documentation for Customer Account API with Hydrogen, it can be referenced here, but the pertinent steps are re-outlined below.
Set up ngrok
ngrok has a free plan but is limited by the number of HTTP requests that can be made monthly. The free plan may be sufficient to get through this migration, but upgrading to a paid plan may be a likely scenario for the long term maintenance of the store, due to the added convenience to create your own domain and to not be limited by your usage. In later steps, the ngrok domain will need to be added officially into Hydrogen settings.
- Set up an ngrok account
- If you have a paid account, add a static domain in your ngrok settings. If you are using the free plan, a random domain is generated for you after every instance as seen in the terminal, e.g.
https://abc123def456.ngrok-free.app
- Install the ngrok CLI
- In a terminal, start ngrok using the following command:
Paid account: ngrok http --domain=<your-ngrok-domain> 8080
Free account: ngrok http 8080
Notes:
- Working off ngrok is only needed for using customer accounts, all other development can still be viewed on
localhost
directly - Stopping or restarting
localhost
does not kill the ngrok instance - If you using the free plan and the ngrok instance is stopped, upon starting again, a new random domain will be generated, thus this new domain will need to replaced with the one added in Hydrogen settings
Update the application setup
For the Customer Account API to recognize your domain as a valid authentication host, edit your Customer Account API settings.
- The panel can be found in
Storefront settings
for the Hydrogen store, then underCustomer Account API
- Under
Application setup
, clickEdit
to edit the endpoints - Under
Callback URI(s)
, clickAdd Callback URI
, add all the/account/authorize
url's prefixed with their domain
- First add
https://<your-main-domain>.com/account/authorize
, e.g.https://storefront.com/account/authorize
- Next add one with the ngrok domain as in
https://<your-ngrok-domain>.app/account/authorize
, e.g.https://abc123def456.ngrok-free.app/account/authorize
If you are using the free ngrok plan, this domain will repeatedly change on every instance, so this value will continually be replaced during development. This is when the paid account will prove convenient.
- Under
JavaScript origin(s)
, clickAdd origin
, and all your domains
- First add the main domain, e.g.
https://storefront.com
- Next add the ngrok domain, e.g.
https://abc123def456.ngrok-free.app
- As mentioned above, this value will likely be replaced out during development if using a free ngrok account
- Under
Logout URI
, clickAdd Logout URI
, and all your domains
- First add the main domain, e.g.
https://storefront.com
- Next add the ngrok domain, e.g.
https://abc123def456.ngrok-free.app
- As mentioned above, this value will likely be replaced out during development if using a free ngrok account
Code Migration
Most steps will link to part of the Blueprint release that it is in reference to. These steps will require copying these code changes as a guide of what exactly to do.
When opening a link, it may take 2-5 seconds for it to scroll to the intended block of code.
Env variables:
Add both PUBLIC_CUSTOMER_ACCOUNT_API_CLIENT_ID
and PUBLIC_CUSTOMER_ACCOUNT_API_URL
to the .env
file, if not already added. The values can be found under the "Ready-only variables" in Hydrogen settings Environment and variables
Add Customer Account client:
- In
server.ts
, addcreateCustomerAccountClient
and passcustomerAccount
intogetLoadContext
[commit] - In
root.tsx
, removecustomerAccessToken
logic and replace withcustomerAccount
client logic [commit] - In
remix.env.d.ts
orenv.d.ts
, add new Env and client types [commit] - In
useCustomer.ts
, remove logic pertaining topreviewModeCustomer
[commit] - Create a new file for the new Customer Account API Graphql queries [commit]
- For Blueprint, the new Graphql file exists in a new
customer-account
folder created underapp/data/graphql
, but matching this folder structure is not important, rather just ensure all Graphql imports from this file are imported correctly after the migration
Customer folder and hooks bulk actions:
- Delete the entire
customer
folder in theapp/lib
folder** - Download this
customer
folder and paste the folder into theapp/lib
folder - For customer hooks, download this
customer
folder and paste the folder into theapp/hooks
folder; then add the export to theindex.ts
file [commit]
When deleting the entire app/lib/customer
folder at once, the assumption is that additional logic has not been added to these files or folders to accommodate custom functionality, i.e. code that is not default on Blueprint. Check your git changes to ensure these deleted files do not contain any pertinent code custom to the store. If so, manual intervention will be required to incorporate them into the migration.
Examples include:
- Additional fields queried with Graphql for the customer. Reference the new Customer Account API
Customer
object to see what Graphql fields are accepted - Additional hooks or logic added for additional customer integrations
Cleanup account components:
- Delete
GuestAccountLayout.tsx
from theapp/components/AccountLayout
folder; then delete its export from theindex.ts
file [commit] - Delete the
Login
folder,Register
folder,Activate.tsx
file andResetPassword.tsx
file from theapp/components/Account
folder; then delete their exports from theindex.ts
file [commit] - In
Layout.tsx
, removeusePreviewModeCustomerInit()
[commit] - In
CustomerAccountLayout.tsx
,customerPending
logic can be removed because it is no longer relevant [commit]
Delete old customer routes and replace with new ones:
- Delete all the current customer routes:
($locale).account.$.tsx
(will be added back in next step)($locale).account.addresses.tsx
(will be added back in next step)($locale).account.orders._index.tsx
(will be added back in next step)($locale).account.orders.$id.tsx
(will be added back in next step)($locale).account.profile.tsx
(will be added back in next step)($locale).account.tsx
(will be added back in next step)($locale).account.activate.$id.$activationToken.tsx
($locale).account.login.multipass.tsx
($locale).account.login.tsx
($locale).account.logout.tsx
($locale).account.register.tsx
($locale).account.reset.$id.$resetToken.tsx
($locale).api.customer.tsx
- Download new/updated account routes and paste each route into the
app/routes
folder
When deleting the files, ensure that any custom functionality specific to the store has not just been wiped by checking the git changes. If so, add it back into the code accordingly.
Update instances of customer email:
- Find instances of
customer?.email
orcustomer.email
and replace the.email
with.emailAddress?.emailAddress
, e.g.customer.emailAddress?.emailAddress
Add useProductById
hook:
- In the
app/hooks/product
folder, adduseProductById.ts
[commit] - Add the export to the
index.ts
file [commit] - Add
($locale).api.product-by-id.tsx
to theapp/routes
folder [commit] - Add this new Graphql query to the Storefront API graphql file [commit]
Update address properties:
- In
AddressForm.tsx
make these changes to the code. Changes include:
- Update
province
andcountry
useState default - Update
countries
useMemo - Add
countriesByShortCode
useMemo - Update
provinces
useMemo - Add
provincesByShortCode
useMemo - Update the
useEffect
- Update
name
for inputs forprovince
,country
, andphone
accordingly - Update
label
forselectedOption
for eachSelect
input
- In
AddressesItem.tsx
make these changes to the code. Changes include:
- Update properties destructured from
address
- Update
name
tofirstName
andlastName
- Map out new
formatted
address
Update order and order properties:
- In
Order.tsx
, destructureorder
fromuseLoaderData
instead ofuseCustomerOrder
, which is now deprecated [commit] - In
OrderAddressAndStatus.tsx
(or wherever address and status info is displayed), make these changes to the code. Changes include:
- Update properties destructured from
order
andshippingAddress
- Update
shippingUrl
- Map out new
formatted
address - Update old variables with corresponding new properties
- In
OrderItem.tsx
, make these changes to the code. Changes include:
- Update properties destructured from
order
anditem
- Update
originalPrice
anddiscountedPrice
useMemo - Fetch full product using the new
useProductById
hook - Update old variables with corresponding new properties
- In
OrderTotals.tsx
, make these changes to the code. Changes include:
- Update
currencyCode
- Update
totals
useMemo
- In
Orders.tsx
, grab customer orders fromcustomer
fromuseCustomer
instead ofuseCustomerOrders
, which is now deprecated [commit]
Add new constants:
- Add new constants to the
constants
file or folder [commit]
Remove Multipass checkout button:
- Delete
MultipassCheckoutButton
fromapp/components/Cart
folder - In
CartTotals.tsx
replaceMultipassCheckoutButton
with aLink
, which simply links to thecheckoutUrl
[commit]
Update customer types:
- Replace types from
@shopify/hydrogen/storefront-api-types
with types from@shopify/hydrogen/customer-account-api-types
, i.e.Customer
(e.g.root.ts
,useCustomer.ts
)Order
(check the order related pages)CustomerAddress
(formerlyMailingAddress
) (check the addressses related pages)
Remove deprecated global state variables:
- In either
SettingsProvider.tsx
orGlobalProvider.tsx
(depending on what version Blueprint the store was built on), remove deprecatedpreviewModeCustomer
variables [commit] - If it exists, delete
usePreviewMode.ts
- In either
context.types.ts
orglobal.types.ts
, delete deprecated types [commit]
Update Analytics components to accommodate new customer object:
- In
Analytics.tsx
,customerPending
can be removed because it is no longer relevant [commit] - For any in use
XYZEvents.tsx
, update thecustomer
prop type from genericRecord<string, any>
toCustomer
type imported from@shopify/hydrogen/customer-account-api-types
. Example - In any relevant
events.ts
orutils.ts
files, swap the following customer properties with the new corresponding property. Example
customer.email
->customer.emailAddress?.emailAddress
customer.numberOfOrders
->flattenConnection(customer.orders)?.length
customer.defaultAddress?.countryCodeV2
->customer.defaultAddress?.territoryCode
customer.defaultAddress?.phone
- >customer.defaultAddress?.phoneNumber
customer.defaultAddress?.provinceCode
->customer.defaultAddress?.zoneCode
- Explicitly type
customer
where needed. Example