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
localhostdue 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
localhostdirectly - Stopping or restarting
localhostdoes 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 settingsfor the Hydrogen store, then underCustomer Account API - Under
Application setup, clickEditto edit the endpoints - Under
Callback URI(s), clickAdd Callback URI, add all the/account/authorizeurl'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/authorizeIf 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.
Required @shopify/hydrogen session.commit() migration:
Only applicable if this migration has not already been implemented. Per migration notes for 2024-07 @shopify/hydrogen, all instances of session.commit() in routes are removed and instead exists only in server.ts. Additional logic is also added into AppSession [commit]
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, addcreateCustomerAccountClientand passcustomerAccountintogetLoadContext[commit] - In
root.tsx, removecustomerAccessTokenlogic and replace withcustomerAccountclient logic [commit] - In
remix.env.d.tsorenv.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-accountfolder 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
customerfolder in theapp/libfolder** - Download this
customerfolder and paste the folder into theapp/libfolder - For customer hooks, download this
customerfolder and paste the folder into theapp/hooksfolder; then add the export to theindex.tsfile [commit] - For the remaining hooks, update the imports from
~/lib/customerto from~/hooks, e.g. convertimport {useCustomerCreateAddress} from '~/lib/customer';toimport {useCustomerCreateAddress} from '~/hooks';
- The hooks include:
useCustomerCreateAddress,useCustomerDeleteAddress,useCustomerLogOut,useCustomerUpdateAddress,useCustomerUpdateProfile
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
Customerobject to see what Graphql fields are accepted - Additional hooks or logic added for additional customer integrations
Cleanup account components:
- Delete
GuestAccountLayout.tsxfrom theapp/components/AccountLayoutfolder; then delete its export from theindex.tsfile [commit] - Delete the
Loginfolder,Registerfolder,Activate.tsxfile andResetPassword.tsxfile from theapp/components/Accountfolder; then delete their exports from theindex.tsfile [commit] - In
Layout.tsx, removeusePreviewModeCustomerInit()[commit] - In
CustomerAccountLayout.tsx,customerPendinglogic 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/routesfolder
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?.emailorcustomer.emailand replace the.emailwith.emailAddress?.emailAddress, e.g.customer.emailAddress?.emailAddress
Add useProductById hook:
- In the
app/hooks/productfolder, adduseProductById.ts[commit] - Add the export to the
index.tsfile [commit] - Add
($locale).api.product-by-id.tsxto theapp/routesfolder [commit] - Add this new Graphql query to the Storefront API graphql file [commit]
Update address properties:
- In
AddressForm.tsxmake these changes to the code. Changes include:
- Update
provinceandcountryuseState default - Update
countriesuseMemo - Add
countriesByShortCodeuseMemo - Update
provincesuseMemo - Add
provincesByShortCodeuseMemo - Update the
useEffect - Update
namefor inputs forprovince,country, andphoneaccordingly - Update
labelforselectedOptionfor eachSelectinput
- In
AddressesItem.tsxmake these changes to the code. Changes include:
- Update properties destructured from
address - Update
nametofirstNameandlastName - Map out new
formattedaddress
Update order and order properties:
- In
Order.tsx, destructureorderfromuseLoaderDatainstead 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
orderandshippingAddress - Update
shippingUrl - Map out new
formattedaddress - Update old variables with corresponding new properties
- In
OrderItem.tsx, make these changes to the code. Changes include:
- Update properties destructured from
orderanditem - Update
originalPriceanddiscountedPriceuseMemo - Fetch full product using the new
useProductByIdhook - Update old variables with corresponding new properties
- In
OrderTotals.tsx, make these changes to the code. Changes include:
- Update
currencyCode - Update
totalsuseMemo
- In
Orders.tsx, grab customer orders fromcustomerfromuseCustomerinstead ofuseCustomerOrders, which is now deprecated [commit]
Add new constants:
- Add new constants to the
constantsfile or folder [commit]
Remove Multipass checkout button and authenticate Checkout URL:
- In
CartTotals.tsxreplaceMultipassCheckoutButtonwith aLink, which links to thecheckoutUrl[commit] - Addendum to step 1: Authenticate the
checkoutUrland instead pass in theauthenticatedCheckoutUrlto the newLink[commit] - Delete
MultipassCheckoutButtonfromapp/components/Cartfolder
Update customer types:
- Replace types from
@shopify/hydrogen/storefront-api-typeswith 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.tsxorGlobalProvider.tsx(depending on what version Blueprint the store was built on), remove deprecatedpreviewModeCustomervariables [commit] - If it exists, delete
usePreviewMode.ts - In either
context.types.tsorglobal.types.ts, delete deprecated types [commit]
Update Analytics components to accommodate new customer object:
- In
Analytics.tsx,customerPendingcan be removed because it is no longer relevant [commit] - For any in use
XYZEvents.tsx, update thecustomerprop type from genericRecord<string, any>toCustomertype imported from@shopify/hydrogen/customer-account-api-types. Example - In any relevant
events.tsorutils.tsfiles, swap the following customer properties with the new corresponding property. Example
customer.email->customer.emailAddress?.emailAddresscustomer.numberOfOrders->flattenConnection(customer.orders)?.lengthcustomer.defaultAddress?.countryCodeV2->customer.defaultAddress?.territoryCodecustomer.defaultAddress?.phone- >customer.defaultAddress?.phoneNumbercustomer.defaultAddress?.provinceCode->customer.defaultAddress?.zoneCode
- Explicitly type
customerwhere needed. Example