RFC: Hydrogen Customer Account API #1189
Replies: 13 comments 39 replies
-
This is great, very excited to start moving over.
|
Beta Was this translation helpful? Give feedback.
-
Is there a way to create a customer account before they order? (we're looking at adding wishlists etc) |
Beta Was this translation helpful? Give feedback.
-
Is it worth considering the use of |
Beta Was this translation helpful? Give feedback.
-
re: codegen, do you know how to run introspection on the Customer Account API graphql endpoint? It seems it needs an access token which isn't practical given you want to be able to introspect for IDE / codegen purposes? {
"errors": "Not a valid access token"
} |
Beta Was this translation helpful? Give feedback.
-
Quick alignment decision on the environment variables naming. We already utilize the following environment variables for the Storefront API:
I would suggest following the same naming structure and slightly deviate from the naming as seen in the description:
If anyone has differing opinion would love to hear them. |
Beta Was this translation helpful? Give feedback.
-
How long will it take for the API to become stable, so we can use it on production? |
Beta Was this translation helpful? Give feedback.
-
We use Remix for our custom storefront (but not Hydrogen), however we've been using Hydrogen as a good reference 🙏. I wonder if you could help with a few questions re: Hydrogen and the Customer Account API:
|
Beta Was this translation helpful? Give feedback.
-
Not sure if this is the correct place to post this, but when using the new Customer Accounts API, if you log in a customer, the cart on the headless site cannot be updated with the customer identification. Specifically, I figured that you can use the customerAccessToken returned during the Token Exchange to update the Cart in the Storefront API with the buyerIdentity Argument, passing the customerAccessToken received in the Customer Accounts API to that field in the buyerIdentity. However, this does not work, as the token is considered invalid. Apparently, there customerAccessToken required in the Storefront API for the cartBuyerIdentityUpdate mutation, is different than the customerAccessToken created by the Customer Accounts API, and a separate one must be created with the Storefront API using username/password mutation. However, if you use the Customer Accounts API, you can't and won't use the username/password mutation. So how can you update the cart with the buyerIdentity using the new Customer Accounts API? Will you provide a basic mutation to do this in the Customer Accounts API? Currently, there doesn't seem to be any sort of Cart Mutations in the Customer Accounts API and the only way to apply the customer to the cart is to redirect them to the checkout with logged_in=true parameter. Then the cart will apply the customer specific options to the cart, and if the customer comes back to the headless site, then the cart will be updated with the customer data. Of course, this is kind of a terrible UI, as you should be able to set the customer on the cart on the headless site without first redirecting to the checkout. |
Beta Was this translation helpful? Give feedback.
-
How long are the refresh tokens good for? Example: A customer logs in to our app, and we save the refresh token to a cookie. Then let's say they come back 24 hours later, will the refresh token still work to get a new access token or does it have an expiry date? |
Beta Was this translation helpful? Give feedback.
-
BTW, according to the docs here: https://shopify.dev/docs/api/customer a request for the refresh token will return a new id_token. However, this is not true. When you send in a request for a refresh_token, you do not get back a new id_token. I assume the id_token from the initial access token request should be used as the id_token? Because the refresh_token endpoint does not send back an id_token per above. |
Beta Was this translation helpful? Give feedback.
-
I'm not sure if this is the place to ask about this, but do you have any idea if we can expect the login page and the verification code email to be translatable to other languages anytime soon? Otherwise I'll scare my clientbase by suddenly changing the website language. Thanks a lot! |
Beta Was this translation helpful? Give feedback.
-
The customer account API pages on Shopify now have a very annoying "Cloudflare checking if you are human" flag for every login and logout. This is very confusing to customers and renders the entire experience totally suboptimal. Sometimes customers can wait a few seconds to see the login. |
Beta Was this translation helpful? Give feedback.
-
Closing this discussion since the API is officially in Hydrogen now |
Beta Was this translation helpful? Give feedback.
-
RFC: Hydrogen Customer Account API
Date: 2023 Aug 4
Shopify has introduced the Customer Account API that allows customers to view their orders and manage their profile. This new API uses an OAuth 2.0 PKCE authorization flow. While this API is powerful, it isn't user friendly for developers that aren't familiar with OAuth. We plan to introduce an abstraction into Hydrogen to make interacting with the Customer Account API easier.
This proposal is mostly available as an example in the Hydrogen repository.
createCustomerClient()
Similar to
createStorefrontClient()
, a Hydrogen app adds support for the Customer Account API by callingcreateCustomerClient()
and adding the result to load context.Authenticating to the Customer Account API
With the customer client now on the load context, we can setup a route with an action to start the authentication process:
A simple Remix form request to this action route starts the authentication:
The
customer.login()
method begins authentication by redirecting to a Shopify hosted login site. If the user successfully logs in, they are redirected back to your custom storefront to a route of your choosing. In that redirect an authorization code is passed. We need to use that code to finish the authentication. Callcontext.customer.authorize()
to finish that process:If
.authorize()
fails, an error Response is thrown. Alternatively you can catch that response to custom redirect the user:Check if the user is logged in
Check if the user is logged in with the
customer.isLoggedInMethod()
Querying the Customer Account API
Query the Customer Account API with the
.query
and.mutate
methods:Refreshing the authorization token
A user might be logged in, but their authorization token is expired. So before querying the API, their token needs to be refreshed. We automatically refresh the token for you when you call
.query
or.mutate
. But if the refresh fails, the user should be considered logged out.Automatically refreshing the authorization token means that the new token also needs to get persisted into the session. This means that any loader or action that you query or mutate data also needs to commit the session. Which is why the previous example has
'Set-Cookie': await context.session.commit()
in the response.Alternatively, you can always commit the session within
server.ts
:Note: If the session is commited on a route, that route cannot be full-page cached. If you commit on every route, then full-page caching is completely disabled.
Logout
Logout by simply implementing an action in a route that calls and returns
customer.logout()
. This will redirect the user to a shopify hosted domain, log them out, and redirect them back to a route of your choice (configured within the admin):On logout, Hydrogen clears out all tokens from the session.
API Reference
Questions
1. How do we support TypeScript codegen?
2. The Storefront API client has both
mutate
andquery
to separate which is automatically cached. We never will automatically cache requests to the Customer Account API, so does it make sense to have both.mutate
and.query
when they are identical?3. How should the Storefront API contextualize requests based on the logged in user? Should we add a simple method
customer.getAuthToken()
and developers manually pass that variable into their storefront queries? Or could we automatically inject the token into Storefront requests like we do for i18n? With either option, how do we make sure the token is up to date? Should we automatically refresh it?4. Is it worth adding higher abstractions for
customer.getOrders()
,customer.update()
and similar functionality. This was implemented in a previous PR: #743Beta Was this translation helpful? Give feedback.
All reactions