create Customer Account Clientutility
The function creates a GraphQL client for querying the Customer Account API. It also provides methods to authenticate and check if the user is logged in.
Anchor to createcustomeraccountclient(options)createCustomerAccountClient(options)
Unique UUID prefixed with associated with the application, this should be visible in the customer account api settings in the Hydrogen admin channel. Mock.shop doesn't automatically supply customerAccountId. Use
npx shopify hydrogen env pull
to link your store credentials.
The object for the current Request. It should be provided by your platform.
The client requires a session to persist the auth and refresh token. By default Hydrogen ships with cookie session storage, but you can use another session storage implementation.
The shop id. Mock.shop doesn't automatically supply shopId. Use npx shopify hydrogen env pull
to link your store credentials
The oauth authorize path. Defaults to .
This is the route in your app that authorizes the customer after logging in. Make sure to call customer.authorize()
within the loader on this route. It defaults to .
Use this method to overwrite the default logged-out redirect behavior. The default handler throws a redirect to with current path as
query param.
Override the version of the API
The path to redirect to after login. Defaults to /account
.
Localization data.
Whether it should print GraphQL errors automatically. Defaults to true
The path to login. Defaults to .
Deprecated. is now stable. Please remove.
The waitUntil function is used to keep the current request/response lifecycle alive even after a response has been sent. It should be provided by your platform.
Anchor to returnsReturns
On successful login, the customer redirects back to your app. This function validates the OAuth response and exchanges the authorization code for an access token and refresh token. It also persists the tokens on your session. This function should be called and returned from the Remix loader configured as the redirect URI within the Customer Account API settings in admin.
Returns CustomerAccessToken if the customer is logged in. It also run a expiry check and does a token refresh if needed.
Creates the fully-qualified URL to your store's GraphQL endpoint.
Check for a not logged in customer and redirect customer to login page. The redirect can be overwritten with option.
Returns if the customer is logged in. It also checks if the access token is expired and refreshes it if needed.
Start the OAuth login flow. This function should be called and returned from a Remix action. It redirects the customer to a Shopify login domain. It also defined the final path the customer lands on at the end of the oAuth flow with the value of the query param. (This is automatically setup unless
option is in use)
Logout the customer by clearing the session and redirecting to the login domain. It should be called and returned from a Remix action. The path app should redirect to after logout can be setup in Customer Account API settings in admin.
Execute a GraphQL mutation against the Customer Account API. This method execute ahead of mutation.
Execute a GraphQL query against the Customer Account API. This method execute ahead of query.
Example code
Anchor to examplesExamples
Examples of how to opt out of default logged-out redirect
Anchor to example-customized-logged-out-behavior-for-the-entire-applicationCustomized logged-out behavior for the entire application
Anchor to example-exampleExample
Throw error instead of redirect
Example
Anchor to example-opt-out-of-logged-out-behavior-for-a-single-routeOpt out of logged-out behavior for a single route
Anchor to example-exampleExample
Handle logged-out ahead of query