---
title: Update buyer identity
description: Use the cart handler to update buyer identity.
source_url:
  html: 'https://shopify.dev/docs/storefronts/headless/hydrogen/cart/buyer-identity'
  md: >-
    https://shopify.dev/docs/storefronts/headless/hydrogen/cart/buyer-identity.md
api_name: hydrogen
---

# Update buyer identity

Cart buyer identity is used for associating customer information with a cart. You should update cart buyer identity when customer information is available:

* Update `countryCode` when a customer changes site currency or locale so the cart will return pricing in the correct currency.

* Update `customerAccessToken` when a customer logs into a customer account so the cart will return selected delivery options.

  This guide shows you how to use the cart handler to update buyer identity.

***

## Requirements

* You've completed the [quickstart guide](https://shopify.dev/docs/storefronts/headless/hydrogen/getting-started).
* You've [set up a cart handler](https://shopify.dev/docs/storefronts/headless/hydrogen/cart/setup).

***

## Handle the buyer identity update

Handle the buyer identity update in an `action`. Use the `cart`, created from [`createCartHandler`](https://shopify.dev/docs/api/hydrogen/latest/utilities/createcarthandler), to handle cart mutation requests to the Storefront API.

## File

## /app/routes/account.login.jsx

##### JavaScript

```jsx
export async function action({request, context}) {
  const {cart, session} = context;
  const formData = await request.formData();

  const email = formData.get('email');
  const password = formData.get('password');

  try {
    // Obtain a customer access token
    // https://shopify.dev/docs/custom-storefronts/building-with-the-storefront-api/customer-accounts#step-3-create-an-access-token
    const customerAccessToken = await doLogin(context, {email, password});
    session.set('customerAccessToken', customerAccessToken);

    // Sync customerAccessToken with existing cart
    const result = await cart.updateBuyerIdentity({customerAccessToken});

    // Update cart id in cookie
    const headers = cart.setCartId(result.cart.id);

    // Update session
    headers.append('Set-Cookie', await session.commit());

    return redirect(params.locale ? `/${params.locale}/account` : '/account', {
      headers,
    });
  } catch (error: any) {
    // handle error
  }
};
```

##### TypeScript

```jsx
export async function action({request, context}: ActionArgs) {
  const {cart, session} = context;
  const formData = await request.formData();

  const email = formData.get('email');
  const password = formData.get('password');

  try {
    // Obtain a customer access token
    // https://shopify.dev/docs/custom-storefronts/building-with-the-storefront-api/customer-accounts#step-3-create-an-access-token
    const customerAccessToken = await doLogin(context, {email, password});
    session.set('customerAccessToken', customerAccessToken);

    // Sync customerAccessToken with existing cart
    const result = await cart.updateBuyerIdentity({customerAccessToken});

    // Update cart id in cookie
    const headers = cart.setCartId(result.cart.id);

    // Update session
    headers.append('Set-Cookie', await session.commit());

    return redirect(params.locale ? `/${params.locale}/account` : '/account', {
      headers,
    });
  } catch (error: any) {
    // handle error
  }
};
```

***

## Next steps

* Learn how to [apply cart notes](https://shopify.dev/docs/storefronts/headless/hydrogen/cart/notes).

***
