--- title: Hydrogen with account component description: Learn how to set up account component on a Hydrogen storefront. source_url: html: >- https://shopify.dev/docs/storefronts/headless/bring-your-own-stack/hydrogen-with-account-component md: >- https://shopify.dev/docs/storefronts/headless/bring-your-own-stack/hydrogen-with-account-component.md --- # Hydrogen with account component Follow this guide to add the [``](https://shopify.dev/docs/api/storefront-web-components/components/shopify-account) component to your Hydrogen storefront, allowing customers to sign in and stay on the storefront, or navigate directly to customer account pages from the storefront. *** ## Requirements * Create a Hydrogen storefront and deploy to Oxygen by following [this tutorial](https://shopify.dev/docs/storefronts/headless/hydrogen/getting-started). * You're familiar with [storefront web components](https://shopify.dev/docs/api/storefront-web-components/getting-started). * You've completed [Using the Customer Account API with Hydrogen](https://shopify.dev/docs/storefronts/headless/building-with-the-customer-account-api/hydrogen). *** ## Step 1: Enable API permissions This requires setting the [`public-access-token`](https://shopify.dev/docs/api/storefront-web-components/components/shopify-store#attributes-propertydetail-publicaccesstoken) attribute on your [``](https://shopify.dev/docs/api/storefront-web-components/components/shopify-store) component. In your store's Shopify admin, open the Hydrogen app. Then select your Hydrogen storefront and navigate to **Storefront settings** > **Storefront API**. Confirm that your Hydrogen app has the following Storefront API permissions: * `unauthenticated_read_customers` * `unauthenticated_read_content` * `unauthenticated_read_product_listings` *** ## Step 2: Pass tokens to the account component To use the `` component, you must provide the following two tokens: * `public-access-token`: Retrieved from `PUBLIC_STOREFRONT_API_TOKEN` in your environment file. * `customer-access-token`: See [Using the Customer Account API with Hydrogen](https://shopify.dev/docs/storefronts/headless/building-with-the-customer-account-api/hydrogen). After the customer is authenticated, pass the customer access token to the `customer-access-token` attribute. Be sure to [update the application setup](https://shopify.dev/docs/storefronts/headless/building-with-the-customer-account-api/hydrogen#update-the-application-setup). In `/app/root.jsx`, replace the `loader` function with the following implementation. This passes `publicAccessToken` and `customerAccessToken` to downstream components. ## /app/root.jsx ```jsx export async function loader(args) { // Start fetching non-critical data without blocking time to first byte const deferredData = loadDeferredData(args); // Await the critical data required to render initial state of the page const criticalData = await loadCriticalData(args); const {storefront, env, customerAccount} = args.context; // Try to get customer access token if logged in (for shopify-account web component) let customerAccessToken = null; try { const isLoggedIn = await customerAccount.isLoggedIn(); if (isLoggedIn) { customerAccessToken = await customerAccount.getAccessToken(); } } catch (error) { // Customer not logged in - that's fine customerAccessToken = null; } return { ...deferredData, ...criticalData, publicStoreDomain: env.PUBLIC_STORE_DOMAIN, publicAccessToken: env.PUBLIC_STOREFRONT_API_TOKEN, customerAccessToken, shop: getShopAnalytics({ storefront, publicStorefrontId: env.PUBLIC_STOREFRONT_ID, }), consent: { checkoutDomain: env.PUBLIC_CHECKOUT_DOMAIN, storefrontAccessToken: env.PUBLIC_STOREFRONT_API_TOKEN, withPrivacyBanner: false, // localize the privacy banner country: args.context.storefront.i18n.country, language: args.context.storefront.i18n.language, }, }; } ``` In the `/app/components/PageLayout.jsx` file, replace `PageLayout` function as following code: ## /app/components/PageLayout.jsx ```jsx /** * @param {PageLayoutProps} */ export function PageLayout({ cart, children = null, footer, header, isLoggedIn, publicStoreDomain, publicAccessToken, customerAccessToken, }) { return ( {header && (
)}
{children}