create Customer Account Client
The createCustomerAccountClient 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)
- Anchor to customerAccountIdcustomer
Account Idcustomer Account Id stringstringrequiredrequired 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. Usenpx shopify hydrogen env pullto link your store credentials.- Anchor to requestrequestrequestCrossRuntimeRequestCrossRuntimeRequestrequiredrequired
The object for the current Request. It should be provided by your platform.
- Anchor to sessionsessionsessionHydrogenSessionHydrogenSessionrequiredrequired
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.
- Anchor to shopIdshop
Idshop Id stringstringrequiredrequired The shop id. Mock.shop doesn't automatically supply shopId. Use
npx shopify hydrogen env pullto link your store credentials- stringstring
The oauth authorize path. Defaults to
.- Anchor to authUrlauth
Urlauth Url stringstring 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.- Anchor to customAuthStatusHandlercustom
Auth Status Handlercustom Auth Status Handler () => DataFunctionValue() => DataFunctionValue Use this method to overwrite the default logged-out redirect behavior. The default handler throws a redirect to
with current path asquery param.- Anchor to customerApiVersioncustomer
Api Versioncustomer Api Version stringstring Override the version of the API
- Anchor to defaultRedirectPathdefault
Redirect Pathdefault Redirect Path stringstring The path to redirect to after login. Defaults to
/account.- Anchor to languagelanguagelanguageLanguageCodeLanguageCode
Localization data.
- Anchor to logErrorslog
Errorslog Errors boolean | ((error?: Error) => boolean)boolean | ((error?: Error) => boolean) Whether it should print GraphQL errors automatically. Defaults to true
- Anchor to loginPathlogin
Pathlogin Path stringstring The path to login. Defaults to
.- Anchor to unstableB2bunstable
B2bunstable B2b booleanboolean Deprecated.
is now stable. Please remove.- Anchor to useCustomAuthDomainuse
Custom Auth Domainuse Custom Auth Domain booleanboolean Set to true when using a custom HTTPS domain (e.g., ngrok, local proxy) instead of the default Hydrogen tunnel for local development. You must manually register your redirect_uri in Customer Account API settings.
- Anchor to waitUntilwait
Untilwait Until WaitUntilWaitUntil 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.
DataFunctionValue
Response | NonNullable<unknown> | nullCrossRuntimeRequest
- headers
{ get?: (key: string) => string | null | undefined; [key: string]: any; } - method
string - url
string
Anchor to returnsReturns
- () => Promise<Response>() => Promise<Response>
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.
- Anchor to getAccessTokenget
Access Tokenget Access Token () => Promise<string>() => Promise<string> Returns CustomerAccessToken if the customer is logged in. It also run a expiry check and does a token refresh if needed.
- Anchor to getApiUrlget
Api Urlget Api Url () => string() => string Creates the fully-qualified URL to your store's GraphQL endpoint.
- Anchor to handleAuthStatushandle
Auth Statushandle Auth Status () => Promise<void>() => Promise<void> Check for a not logged in customer and redirect customer to login page. The redirect can be overwritten with
option.- Anchor to isLoggedInis
Logged Inis Logged In () => Promise<boolean>() => Promise<boolean> Returns if the customer is logged in. It also checks if the access token is expired and refreshes it if needed.
- Anchor to loginloginlogin(options?: LoginOptions) => Promise<Response>(options?: LoginOptions) => Promise<Response>
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 unlessoption is in use)- Anchor to logoutlogoutlogout(options?: LogoutOptions) => Promise<Response>(options?: LogoutOptions) => Promise<Response>
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.
- Anchor to mutatemutatemutate<TData = any>( mutation: string, options: CustomerAccountQueryOptionsForDocs, ) => Promise<TData><TData = any>( mutation: string, options: CustomerAccountQueryOptionsForDocs, ) => Promise<TData>
Execute a GraphQL mutation against the Customer Account API. This method execute
ahead of mutation.- Anchor to queryqueryquery<TData = any>( query: string, options: CustomerAccountQueryOptionsForDocs, ) => Promise<TData><TData = any>( query: string, options: CustomerAccountQueryOptionsForDocs, ) => Promise<TData>
Execute a GraphQL query against the Customer Account API. This method execute
ahead of query.
LoginOptions
- acrValues
string - countryCode
CountryCode - locale
string - loginHint
string - loginHintMode
string - uiLocales
LanguageCode
LogoutOptions
- headers
Add custom headers to the logout redirect.
HeadersInit - keepSession
If true, custom data in the session will not be cleared on logout.
boolean - postLogoutRedirectUri
The url to redirect customer to after logout, should be a relative URL. This url will need to included in Customer Account API's application setup for logout URI. The default value is current app origin, which is automatically setup in admin when using `--customer-account-push` flag with dev.
string
CustomerAccountQueryOptionsForDocs
- variables
The variables for the GraphQL statement.
Record<string, unknown>