Shopify customer account extensions allow developers to build custom apps and UIs in Shopify's order status portal. Details about the new customer extensibility features can be found in the Shopify docs.
To build a customer account UI extension with Gadget, you need to make sure that requests made to your Gadget API from the extension are secure, and that the customer can only access their own data.
Limited to new customer accounts
Shopify's customer account UI extensions and customer authentication in Gadget are currently only available to stores using Shopify's new
customer accounts. Classic customer accounts are not supported.
Enable customer account authentication
To set up your Gadget API to handle requests from the customer account UI extension:
Go to Settings > Plugins > Shopify in the Gadget editor
What does enabling customer account authentication do?
Enabling customer account authentication on a Gadget app will make the following changes to your app:
Add a new shopify-storefront-customersaccess role to accessControl/permissions that allows you to manage authorization for customer requests
This role is granted read access to any customer data that is stored in the app
A tenancy filter is automatically created and enforced so that customers can only read their own data
Add a relationship from the session model to shopifyCustomer so session records can be created for customers making requests
A customer account login trigger is added to the shopifyCustomer model's create action so a shopifyCustomer record can be automatically retrieved from Shopify if the record does not already exist in your app's database
Customer data tenancy
In most cases, customers should only access their own data.
To ensure this, Gadget automatically sets up customer data tenancy for the following Shopify models when you enable customer account authentication:
shopifyCheckout
shopifyCompanyContact
shopifyCustomerAddress
shopifyCustomerMergeable
shopifyCustomerPaymentMethod
shopifyDraftOrder
shopifyGiftCard
shopifyOrder
shopifySubscriptionContract
For any other models, including custom models, you will need to manually add customer data tenancy.
Set up customer multi-tenancy manually
You must do three things to enable multi-tenancy for customers on your custom models:
Add a relationship between your custom model and the shopifyCustomer model
Add the preventCrossShopDataAccess function to the run function of your custom model's actions
Add a tenancy filter to your custom model
Using preventCrossShopDataAccess
You can use the preventCrossShopDataAccess function, imported from the gadget-server package, to ensure that customers can only access their data in your custom model's actions.
This function will automatically be added to any custom actions created on Shopify data models.
An example of how to use this function in a custom blog model's create action:
api/models/blog/actions/create.js
JavaScript
1// add `preventCrossShopDataAccess` to the import statement
Adding a tenancy filter to the access control configuration for your custom model will make sure that customers can only access their own data.
These filtered model permissions are written in Gelly, Gadget's data access language.
For example, this Gelly snippet could be used to filter a customerOffer model data by customer. The customerOffer model has a customer relationship field that relates to the shopifyCustomer model.
Example of a tenancy filter for a custom model
gelly
filter ($session: Session)onCustomerOffer[
where customerId ==$session.shopifyCustomerId
]
For more information on tenancy filters and filtered model permissions, see the access control guide.
Disabling customer multi-tenancy for a Shopify model
You may want to disable customer multi-tenancy for a Shopify model but still enforce shop multi-tenancy with preventCrossShopDataAccess.
To do this, set the enforceCustomerTenancy option to false in the preventCrossShopDataAccess function call:
Example of disabling customer multi-tenancy for a Shopify model
For more information on the preventCrossShopDataAccess function, see the Gadget API reference.
Setting up the Shopify CLI extension
Now you can set up a Shopify CLI app with a customer account UI extension and make requests to your Gadget API. Follow these steps to set up the CLI app:
If you are using a separate Shopify CLI project to build extensions, you will also need to install the @gadgetinc/react package.
Initialize a new client, and wrap the extension component with the Provider component from @gadgetinc/shopify-extensions and pass it the api client and sessionToken
45// use ready to pause hooks until the API client is ready to make authenticated requests
46pause:!ready,
47}
48);
49
50// do not display anything if the data is still being fetched or there is an error
51if(fetching)returnnull;
52if(error){
53console.error(error);
54returnnull;
55}
56
57// disable the button if an issue has already been reported
58return(
59<Buttondisabled={!!orderIssue.issueId}>
60{orderIssue.issueId?"Report submitted":"Report a problem"}
61</Button>
62);
63}
This is an example of how to use the useFindOne hook to fetch custom data from your Gadget API and use it in your extension.
The Provider component will automatically pass the sessionToken to the api client. This will allow customers to make authenticated requests to your Gadget API, and grants them the shopify-storefront-customers role.
A ready boolean is used to pause hook calls until the api client is ready to make authenticated requests.
Not using React?
If you are not using React to build your extensions, you can still use the @gadgetinc/shopify-extensions package to handle
authentication and make requests to your Gadget API. Check out the package
docs for more information.
Disabling customer account authentication
To disable customer account authentication you can edit your existing Shopify connection in the Gadget editor and disable the Customer account authentication setting.
Removing customer account authentication will automatically remove the customer account login trigger from the shopifyCustomer model's create action.