Skip to main content

Customer account server handlers

The createCustomerAccountServerHandlers function creates route handlers for customer account login, authorization, token refresh, and logout. Use the handlers to sign customers in and out from four account routes, and to link the signed-in customer to their cart.

Import createCustomerAccountServerHandlers from @shopify/hydrogen/customer-account.

Pass a session from createCustomerSession and register the handlers with handleShopifyRoutes.


Creates the customer account routes under /account for sign-in, the OAuth callback, token refresh, and logout. Add the returned object to the handlers option of handleShopifyRoutes.

The login route redirects to Shopify's login page. The authorize and refresh routes redirect back to a same-origin path in your app. The logout route redirects to Shopify's logout endpoint when the session has an ID token, and to the post-logout URL otherwise. Each redirect commits the session and sends cache-control: no-store.

Link to these paths with full-page navigation, such as a plain <a> link or a <form>. Client-side navigation components can't follow these redirects.

Anchor to options
options
required

The customer session, the redirect paths, and optional cart handlers for the account routes.

|

The login, authorize, refresh, and logout routes.

Examples

TypeScript

import {
createCustomerAccountServerHandlers,
createCustomerSession,
} from "@shopify/hydrogen/customer-account";

const customerSession = createCustomerSession({
shopId: process.env.SHOP_ID!,
customerAccountApiClientId: process.env.PUBLIC_CUSTOMER_ACCOUNT_API_CLIENT_ID!,
});

export const customerAccountHandlers = createCustomerAccountServerHandlers({
customerSession,
defaultPostLoginRedirectPathname: "/account",
loginFailedRedirectPath: "/account?login=failed",
postLogoutRedirectUri: "/",
});

Import each route path as a constant from @shopify/hydrogen/customer-account:

  • /account/login: CUSTOMER_ACCOUNT_LOGIN_PATH
  • /account/authorize: CUSTOMER_ACCOUNT_AUTHORIZE_PATH
  • /account/refresh: CUSTOMER_ACCOUNT_REFRESH_PATH
  • /account/logout: CUSTOMER_ACCOUNT_LOGOUT_PATH

Was this page helpful?