Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 61 ยท Cart, Checkout and Customer Pages

Customer Accounts and Account Pages

Understand Shopify's new and classic customer accounts, the customer templates, login, register and address forms, order history, and safe redirects.

Customer accounts let shoppers sign in, see their orders, keep addresses and reorder faster. For a theme developer the first surprise is that there are two very different systems behind the word "account", and which one a store uses decides how much of the account area you can build. Many an hour has been lost styling a login template that the store never shows.

This lesson explains the two account systems, the classic customer templates and the forms they use, how order history and addresses are rendered, how to link to account pages correctly and how redirects after login work. It follows Cart, Discounts and the Path to Checkout and uses the template ideas from JSON Templates and Online Store 2.0.

Account features have changed quickly in recent years and the details depend on the store's settings. The theme code below is read-only, because the customer object and the form tags exist only on Shopify, and it targets current Online Store 2.0 themes. Verify the behavior in a development store before promising it to a merchant. The stand-ins are plain JavaScript.

New and classic customer accounts

New customer accounts are hosted by Shopify. Shoppers sign in with a one-time code sent to their email (and, depending on the store, other methods), and the account pages (orders, profile, addresses) are Shopify's pages, not your Liquid. Your theme's contribution is limited: it links to them, and it can be styled only through the store's branding settings and extensions.

Classic customer accounts are rendered by your theme. They use passwords, and every page is a template in your theme folder that you can design completely.

Because merchants can switch between the two, a good theme does both things at once: it uses the routes Shopify gives you for links (so they point to the right place in either system) and it still ships the classic templates, so a store on classic accounts works.

flowchart TB
  Q{"Which accounts does the store use?"}
  Q -->|"New customer accounts"| N["Shopify-hosted pages<br/>one-time code sign-in"]
  Q -->|"Classic customer accounts"| C["Theme templates<br/>customers/login, account, order..."]
  L["Your theme links with routes.account_login_url<br/>and routes.account_url"] --> Q

Link through the routes objects and the right system answers, whichever one is switched on.

The classic customer templates

These templates live in templates/customers/:

Template Purpose
login sign in; also holds the password recovery form
register create an account
account dashboard and order history
order one order's details
addresses list, add, edit and delete addresses
reset_password choose a new password after an email link
activate_account set a password for an invited account

In Online Store 2.0 these can be JSON templates composed of sections (a main-login section, a main-account section and so on), like any other template, which is how Dawn organizes them. The customer-related objects are only filled in on these pages, so code that needs the signed-in customer elsewhere should use the global customer object, which is nil for guests. The theme is cached, so be careful with per-user output on cacheable pages and see Liquid Performance for the reasoning.

Linking to account pages

Never type /account/login by hand. Use the route objects, which also handle store-specific roots and the account system in use:

LIQUID
{%- if shop.customer_accounts_enabled -%}
  {%- if customer -%}
    <a href="{{ routes.account_url }}">Account ({{ customer.first_name | default: customer.email }})</a>
    <a href="{{ routes.account_logout_url }}">Log out</a>
  {%- else -%}
    <a href="{{ routes.account_login_url }}">Log in</a>
    <a href="{{ routes.account_register_url }}">Create account</a>
  {%- endif -%}
{%- endif -%}

shop.customer_accounts_enabled is false when the merchant turned accounts off, so the links disappear instead of leading to a dead end. Beware a trap: if the header is cached for everyone, customer is not reliable there. Real themes either render this fragment uncached, or print a neutral "Account" link that goes to routes.account_url and lets Shopify decide whether to show the dashboard or the login page.

The login and register forms

Account forms use the form tag with a type. Shopify adds the right action, the method, hidden security fields and the CSRF-like protection for you:

LIQUID
{% form 'customer_login', novalidate: 'novalidate' %}
  {%- if form.errors -%}
    <div role="alert" tabindex="-1" id="LoginErrors">
      <h2>Please fix the following</h2>
      <ul>
        {%- for field in form.errors -%}
          <li>
            <a href="#CustomerLogin-{{ field }}">{{ form.errors.translated_fields[field] | capitalize }} {{ form.errors.messages[field] }}</a>
          </li>
        {%- endfor -%}
      </ul>
    </div>
  {%- endif -%}

  <label for="CustomerLogin-email">Email</label>
  <input type="email" name="customer[email]" id="CustomerLogin-email"
         autocomplete="email" required>

  <label for="CustomerLogin-password">Password</label>
  <input type="password" name="customer[password]" id="CustomerLogin-password"
         autocomplete="current-password" required>

  <input type="hidden" name="return_to" value="{{ routes.account_url }}">
  <button type="submit">Sign in</button>
{% endform %}

The registration form uses the create_customer type with customer[first_name], customer[last_name], customer[email] and customer[password]. Password recovery uses recover_customer_password and shows a success state with form.posted_successfully?. Use proper autocomplete values (email, current-password, new-password): they let password managers fill the fields, and they are an accessibility win. The error list pattern is explained in Forms: Contact, Newsletter and Custom Forms.

Security basics apply: never log or echo passwords, never build your own login endpoint, and never store credentials in JavaScript. For the wider picture, read Security in Themes and Shop Security.

Redirects after login

A shopper who clicks "Log in" from a protected-feeling page expects to land back where they were. Two mechanisms are common:

  • a return_to value in a hidden field (or on the page's query string) that tells Shopify where to go after a successful sign-in;
  • a checkout_url query when the shopper signs in during checkout.

The destination should be a local path on the same store. Never accept a full URL from untrusted input and forward the shopper there, because that is an open redirect and a phishing tool. The stand-in below shows a safe check.

Order history and the account page

On the classic account template, the customer's orders are in customer.orders, and long histories need pagination:

LIQUID
{% paginate customer.orders by 10 %}
  {%- if customer.orders.size > 0 -%}
    <table>
      <caption class="visually-hidden">Order history</caption>
      <thead>
        <tr>
          <th scope="col">Order</th><th scope="col">Date</th>
          <th scope="col">Payment</th><th scope="col">Fulfillment</th>
          <th scope="col">Total</th>
        </tr>
      </thead>
      <tbody>
        {%- for order in customer.orders -%}
          <tr>
            <th scope="row"><a href="{{ order.customer_url }}">{{ order.name }}</a></th>
            <td>{{ order.created_at | date: '%b %d, %Y' }}</td>
            <td>{{ order.financial_status_label }}</td>
            <td>{{ order.fulfillment_status_label }}</td>
            <td>{{ order.total_price | money }}</td>
          </tr>
        {%- endfor -%}
      </tbody>
    </table>
    {{ paginate | default_pagination }}
  {%- else -%}
    <p>You have not placed any orders yet.</p>
  {%- endif -%}
{% endpaginate %}

The order object (on templates/customers/order) additionally has line_items, shipping_address, billing_address, subtotal_price, total_price and tracking information on fulfillments. Dates should go through date with a format and, for a global audience, ideally a localized format (see Translations, Markets and Localization). The lifecycle behind those statuses is explained in The Order Lifecycle.

Addresses

The address page lists customer.addresses and offers a form of type customer_address for adding or editing one. Field names use the address[...] convention, and the edit form's id includes address.id. For international shoppers, do not assume US-style fields: some countries have no provinces, postal codes may be optional and name order differs. Shopify ships a country/province selector script and customer.default_address for the preselected one. Offer autocomplete tokens (given-name, address-line1, postal-code, country) so browsers can help.

Stand-in: a login handler with errors and safe redirects

The first stand-in plays the part of the server for a classic login form: it validates fields, returns errors in the same shape the template loops over, rejects bad credentials with a generic message and approves only safe redirect targets.

JS Stand-in: login handler with errors and safe return_to

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The last two attempts both succeed, but the external return_to is replaced by /account. A real store never lets a successful login bounce a shopper to a foreign site.

Stand-in: rendering an order history page

The second stand-in does what the Liquid table above does: it paginates orders, formats dates and money, and shows an empty state. Only the language differs.

JS Stand-in: order history with pagination

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Page 3 shows only the three oldest orders because 23 orders at 10 per page make three pages.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A merchant uses new customer accounts. What happens to your theme's templates/customers/login template?

    New customer accounts are hosted by Shopify. Your theme still links to them with routes, and keeps the classic templates for stores that use classic accounts.
  2. 2. Why link with routes.account_login_url instead of typing /account/login?

    The route objects give the correct URL for the store and the account system, so links keep working if the merchant changes settings.
  3. 3. What is wrong with redirecting to whatever return_to value is supplied?

    Only local paths on the same store should be accepted. Anything else could be a phishing link disguised behind a trusted login.
  4. 4. Which autocomplete value helps password managers on a registration form?

    new-password marks a field where the shopper creates a password, so managers can suggest a strong one. current-password is for sign-in.

Summary

  • Stores use either Shopify-hosted new customer accounts or theme-rendered classic accounts; support both by linking through routes.
  • Classic accounts are templates under templates/customers/ with forms of types such as customer_login, create_customer and customer_address.
  • Check shop.customer_accounts_enabled, and remember that cached pages cannot safely print per-customer data.
  • Redirect only to local paths, and use generic login errors.
  • Order history uses customer.orders with pagination; addresses need international-friendly fields and autocomplete tokens.

Next, learn the form tag in depth, including contact and newsletter forms, in Forms: Contact, Newsletter and Custom Forms.