Skip to main content

Build pre-auth order status UI extensions

In this tutorial, you'll create two separate customer account UI extensions, with each using different targets to demonstrate the available authentication flows:

  • An order action menu item that opens a modal and prompts the customer to add a note to their order after logging in.
  • An extension on the Order status page that displays how many loyalty points were earned from the purchase, in a card at the top of the order summary.
Order Action Menu Target
Order Summary Target

In this tutorial, you'll learn how to do the following tasks:

  • Use the authenticationState API to get the authorization context of a customer.
  • Create an order action menu extension that opens a modal and uses the seamless login method to trigger customer authentication.
  • Create an extension that's rendered inline on the Order status page and uses the requireLogin API to trigger customer authentication.

Requirements

Project

Anchor to Order action menu and seamless loginOrder action menu and seamless login

Anchor to Create a customer account UI extensionCreate a customer account UI extension

To create a customer account UI extension, you can use Shopify CLI, which generates starter code for building your extension and automates common development tasks.

  1. Navigate to your app directory:

    Terminal

    cd <directory>
  2. Run the following command to create a new customer account UI extension:

    Terminal

    shopify app generate extension --template customer_account_ui --name customer-account-ui-extension
  3. Select a language for your extension. You can choose from TypeScript, JavaScript, TypeScript React, or JavaScript React.

    Tip

    TypeScript or JavaScript is suitable for smaller projects that require a more straightforward API. TypeScript React or JavaScript React is suitable when you want an easy model for mapping state updates to UI updates. With JavaScript or TypeScript, you need to map state updates yourself. This process is similar to writing an application targeting the DOM, versus using react-dom.

    You should now have a new extension directory in your app's directory. The extension directory includes the extension script at src/index.{file-extension}. The following is an example directory structure:

    Customer account UI extension file structure

    └── my-app
    └── extensions
    └── my-customer-account-ui-extension
    ├── src
    │ └── CustomerAccount.jsx OR CustomerAccount.js // The index page of the customer account UI extension
    ├── locales
    │ ├── en.default.json // The default locale for the customer account UI extension
    │ └── fr.json // The locale file for non-regional French translations
    ├── shopify.extension.toml // The config file for the customer account UI extension
    └── package.json
  1. Start your development server to build and preview your app:

    Terminal

    shopify app dev

    To learn about the processes that are executed when you run dev, refer to the Shopify CLI command reference.

  2. Press p to open the developer console. In the developer console page, click on the preview link for your extension.

Anchor to Set up the target for your extensionSet up the target for your extension

Set up the target for your customer account UI extension. Extension targets control where your extension renders in the customer account flow.

This example code uses the following targets:

In your extension's shopify.extension.toml configuration file, for each of the targets, create an [[extensions.targeting]] section with the following information:

  • target: An identifier that specifies where you're injecting code into Shopify.
  • module: The path to the file that contains the extension code.

Info

Whenever you edit your extension configuration file, you need to restart your server for the changes to take effect.

Anchor to Create files for your targetsCreate files for your targets

Create files in your extension's src directory for each of your targets.

In this example, you'll create a file for the order action menu item extension and a file for the order action modal extension. The filenames must match the module paths you specified.

Anchor to Build the order action menu item and modalBuild the order action menu item and modal

Now you'll render the Add note order action button. The button isn't being passed a to or onPress prop. This is how we know to connect this button to the order action modal extension.


Order action menu

Anchor to Build the modal's UIBuild the modal's UI

Use CustomerAccountAction to extend the target. Here, you'll use Form, TextField, and Button components to add a note to the order.


Customer account UI extensions are limited to specific UI components exposed by the platform for security reasons. You can use checkout UI components and customer account UI components to create a UI that feels seamless within the customer accounts experience, and that inherits a merchant's brand settings.


modal open

Anchor to Make an API call to store the order note once the customer is logged inMake an API call to store the order note once the customer is logged in

The example code uses a comment line to mock the adding note request call. In a production-ready application, make an API call to your server.

Anchor to Preview the extensionPreview the extension

Preview your extension to make sure that it works as expected.

Run the Shopify CLI dev command to build your app and preview it on your development store.

  1. In a terminal, navigate to your app directory.

  2. Either start or restart your server to build and preview your app:

    Terminal

    shopify app dev
  3. If prompted, select a development store.

  4. Press p to open the developer console.

  5. In the developer console page, click the preview link for one of your extension targets.

The customer accounts experience opens.

  1. Ensure that you're logged out.

  2. Copy the URL of Order status page from a recent order confirmation email, and paste it to the browser directly to be taken to the pre-auth Order status page, where your extension will render.

  3. Click the Add Note button, and you will be redirected to the login page.

  4. After you login, you will see the modal opened automatically where you can leave a note.


Order action menu

modal open

Info

To test extensions in pre-authenticated state, the customer shouldn't be logged in. Copy and paste the URL of the Order status page from the order confirmation email is the best way to test it.

By default, the URL is valid for five uses or two weeks after the order is placed, whichever limit is reached first. If the URL expires, you will need to place a new order to receive a new one.

Anchor to Inline extension on the ,[object Object], page, and ,[object Object]Inline extension on the Order status page, and requireLogin

Anchor to Repeat Step 1 to create a new customer account UI extensionRepeat Step 1 to create a new customer account UI extension

Anchor to Set up the target for your extensionSet up the target for your extension

Set up the target for your customer account UI extension. Extension targets control where your extension renders in the customer account flow.

The example code uses customer-account.order-status.block.render to render a card in the order summary on the Order status page.

In your extension's configuration file, for the customer-account-order-status-block-render target, create an [[extensions.targeting]] section with the following information:

target: An identifier that specifies where you're injecting code into Shopify.

module: The path to the file that contains the extension code.

Info

Whenever you edit your extension configuration file, you need to restart your server for the changes to take effect.

Anchor to Create files for your targetsCreate files for your targets

Create files in your extension's src directory for each of your targets.

In this example, you'll create a file for the order status block extension. The filenames must match the module paths you specified.

Anchor to Order status block extensionOrder status block extension

Anchor to Render loyalty points cardRender loyalty points card

Using UI extension components, add a card to the Order status page to let the customer know how many loyalty points they've earned for the order.

Anchor to Use the ,[object Object], API to direct the customer to loginUse the requireLogin API to direct the customer to login

Anchor to Determine the authentication state with the ,[object Object], APIDetermine the authentication state with the authenticationState API

Using the authenticationState API to get the current Order status page authentication state. You'll use this later in the tutorial.

Anchor to Render pre-authenticated state contentRender pre-authenticated state content

The extension adds a View rewards link to the card. Use the requireLogin API to direct the customer to login when the link is clicked. This method routes the customer back to the Order status page. The customer will need to re-initiate the action after they're logged in.


Loyalty points in pre-authenticated state

Anchor to Render fully authenticated state contentRender fully authenticated state content

In this example, the number of points is hardcoded.


Loyalty points in the fully authenticated state

Anchor to Preview the extensionPreview the extension

Preview your extension to make sure that it works as expected.

Run the Shopify CLI dev command to build your app and preview it on your development store.

  1. In a terminal, navigate to your app directory.

  2. Either start or restart your server to build and preview your app:

    Terminal

    shopify app dev
  3. If prompted, select a development store.

  4. Press p to open the developer console.

  5. In the developer console page, click the preview link for one of your extension targets.

The customer accounts experience opens.

  1. Place an order, if you haven't already.

  2. Click View your order from the order confirmation email to access the pre-authenticated Order status page. Don't log in.

  3. Click the View rewards link, and you will be redirected to the login page.

  4. After you login, you will be redirected back to the Order Status page and see your points balance.


Loyalty points in pre-authenticated state

Loyalty points in fully authenticated state

Info

To test extensions in the pre-authenticated state, click View your order from the order confirmation email to access the pre-authenticated Order status page. Don't log in.

By default, the URL only works for 5 times and within 2 weeks. So if the URL is expired, you need to create a new order to get a new URL.

Was this page helpful?