Medusa is an open source headless commerce platform that aims to provide developers with a great developer experience. Although it provides Next.js and Gatsby storefronts to use, you can also use any storefront of your choice.
To make your developer experience even easier, Medusa provides a client NPM package that you can use with JavaScript and Typescript frameworks.
In this tutorial, you’ll learn how to install and use the Medusa JS Client in your storefront to implement a customer sign-up and profile flow.
You can find the code for this tutorial in this GitHub repository.
Prerequisites
This tutorial assumes you already have a Medusa server installed. If not, please follow along the quickstart guide.
Furthermore, this tutorial uses the Next.js starter to implement the customer profile. However, the tutorial will focus on how to use the Medusa JS Client in particular. So, you can still follow along if you are using a different framework for your storefront.
Install the Medusa JS Client
In your storefront project’s directory, install the Medusa JS client if you don’t have it installed already:
If you’re using our Next.js or Gatsby starters, then you don’t need to install it.
Initialize Client
In the Next.js starter, the client is initialized in
and you can just import it into your components. However, if you’re using your custom storefront here’s how you can initialize the Medusa client:Copy to clipboardutils/client.js
Where
is the URL to your Medusa server.Copy to clipboardBACKEND_URL
You can then use the methods and resources in the client to send all types of requests to your Medusa server which you’ll see later in this tutorial.
Add Styles
This step is optional and can be skipped. To add some styling for the rest of the tutorial, you can create the file
with this content.Copy to clipboardstyles/customer.module.css
Add Customer to Context
In the Next.js starter, the
holds all variables and methods important to the store like theCopy to clipboardStoreContext
object.Copy to clipboardcart
In this section, you’ll add a variable and a method to the context:
andCopy to clipboardcustomer
respectively.Copy to clipboardsetCustomer
In the
variable inCopy to clipboarddefaultStoreContext
add the following:Copy to clipboardcontext/store-context.js
Then, in the
function, add a case for theCopy to clipboardreducer
action:Copy to clipboardsetCustomer
Inside the
function, add inside theCopy to clipboardStoreProvider
under the cart initialization the following to retrieve the customer if they are logged in and set it in the context:Copy to clipboarduseEffect
Notice here that you’re using
.Copy to clipboardclient.auth.getSession
has been initialized earlier in the file, so if you’re implementing this in your own storefront you’d need to initialize it before this code snippet.Copy to clipboardclient
Then, you use
which allows you to retrieve the current logged in customer, if there is any.Copy to clipboardauth.getSession
If the customer is logged in and is returned in the response, you set it in the context, otherwise you set the customer to
.Copy to clipboardnull
Then, add a new function inside
to handle setting the customer:Copy to clipboardStoreProvider
This will just dispatch the action to the reducer which will set the customer object in the context.
Finally, add the
function to theCopy to clipboardsetCustomer
prop ofCopy to clipboardvalue
returned inCopy to clipboardStoreContext.Provider
:Copy to clipboardStoreProvider
Create Sign Up and Sign In Pages
In this section, you’ll create the sign up and sign in pages to allow the customer to either create an account or log in with an existing one.
Sign Up Page
Create the file
with the following content:Copy to clipboardpages/sign-up.js
In this page, you use Formik and Yup to create and validate the form. This form has 4 fields: email, first name, last name, and password. These fields are required to sign up a customer in Medusa.
The important bit here is the part that uses the Medusa client in the
function passed toCopy to clipboardonSubmit
:Copy to clipboarduseFormik
You first initialize the Medusa client. You use the utility function in
to do that, but if you don’t have that in your storefront you can replace it with the initialization mentioned earlier in the tutorial.Copy to clipboardutils/client.js
Then, you use
which will send a request to the create customer endpoint. This endpoint requires theCopy to clipboardclient.customers.create
,Copy to clipboard
,Copy to clipboardfirst_name
, andCopy to clipboardlast_name
fields.Copy to clipboardpassword
If the sign up is successful, the
object will be returned and a session token will be saved in the cookies to keep the user logged in. You use theCopy to clipboardcustomer
object to set the customer in the context.Copy to clipboardcustomer
Notice also that at the beginning of the component you check if the customer is already logged in and redirect them to the home page in that case:
Sign In Page
Next, create the file
with the following content:Copy to clipboardpages/sign-in.js
This page also makes use of Formik and Yup to create and validate the form. This form only needs an email and a password.
The important bit is in the
function passed toCopy to clipboardonSubmit
:Copy to clipboarduseFormik
Just like before, you start by initializing the Medusa client. Then, you authenticate the customer using
which calls the Authenticate Customer endpoint. This endpoint requires 2 parameters:Copy to clipboardclient.auth.authenticate
andCopy to clipboard
.Copy to clipboardpassword
If successful, a customer object is returned which you use to set the customer in the store context using
. A cookie will also be set to maintain the login session for the customer.Copy to clipboardsetCustomer
Add Links to Navigation Bar
Finally, in
, initialize the customer from theCopy to clipboardcomponents/layout/nav-bar.jsx
:Copy to clipboardStoreContext
Then, in the returned JSX add the following to add a link to the new pages:
Notice that you also add a link to the customer profile, which you’ll implement in the next section.
Test it Out
Make sure that the Medusa server is running first. Then, run the server for your storefront:
If you open
now, you’ll see that there are 2 new links in the navigation bar for sign up and sign in.Copy to clipboardlocalhost:8000
You can try clicking on Sign Up and registering as a new user.
Or click Sign In and log in as an existing user.
Once you’re logged in, you should be redirected back to the home page and you should see a Profile link in the navigation bar.
Add Customer Profile
The customer profile will have 3 pages: Edit customer info, view orders, and view addresses.
Create Profile Layout
You’ll start by creating a layout that all profile pages will use.
Create
with the following content:Copy to clipboardcomponents/layout/profile.jsx
This first checks the customer and context to see if the user is logged in. Then, it displays a sidebar with 3 links, and the main content displays the children.
Create Edit Profile Page
The edit profile page will be the main customer profile page. Create the file
with the following content:Copy to clipboardpages/customer/index.js
This is very similar to the sign-up form. You use Formik and Yup to create and validate the form. The form has 4 inputs:
,Copy to clipboard
,Copy to clipboardfirst_name
, andCopy to clipboardlast_name
which is optional.Copy to clipboardpassword
The important bit here is the part in the
function passed toCopy to clipboardonSubmit
:Copy to clipboarduseFormik
Just like you’ve done before, you start by initializing the Medusa client. Then, if the password is not set, you remove it from the list of values since you’ll be passing it to the server as-is. You should only pass the password if the customer wants to change it.
To update the customer info, you can use
which sends a request to the Update Customer endpoint. This endpoint accepts a few optional parameters includingCopy to clipboardclient.customers.update
,Copy to clipboard
,Copy to clipboardlast_name
, andCopy to clipboardfirst_name
.Copy to clipboardpassword
If the customer info is updated successfully, a customer object is returned which you use to set the updated customer in the context. You also show an alert to the customer that their account is now updated.
If you click on the Profile link in the navigation bar now, you’ll see the profile page with the sidebar and the edit profile page as the first page.
Try updating any of the information and click Save. You should then see an alert to let you know that it’s been updated successfully.
Orders Page
The next page you’ll create is an orders page which will display the customer’s orders.
Create a new file
with the following content:Copy to clipboardpages/customer/orders.js
You first create an
state variable which starts out as empty. You also have aCopy to clipboardorders
state variable to keep track of the number of pages available. Then, inCopy to clipboardpages
, you retrieve the orders using the Medusa client:Copy to clipboarduseEffect
After initializing the client, you retrieve the orders of the customer using
. This method sends a request to the Retrieve Customer Orders endpoint. This endpoint accepts 4 fields:Copy to clipboardclient.customers.listOrders
,Copy to clipboardlimit
,Copy to clipboardoffset
, andCopy to clipboardfields
. Here, you just useCopy to clipboardexpand
andCopy to clipboardlimit
.Copy to clipboardoffset
indicates how many orders should be retrieved per page, andCopy to clipboardlimit
indicates how many orders to skip from the beginning to get the orders of the current page.Copy to clipboardoffset
This request returns the list of orders as well as additional fields important for pagination including
which indicates the total count of the orders andCopy to clipboardcount
which is the current limit set.Copy to clipboardlimit
You set the
state variable to the orders returned from the method and you set the number of pages based on theCopy to clipboardorders
andCopy to clipboardcount
fields.Copy to clipboardlimit
Finally, you display the
in a table showing the ID, total, and status. You also show pagination links for “Previous” and “Next” if applicable for the page. This is done for the simplicity of the tutorial.Copy to clipboardorders
If you open the orders page now, you’ll see the list of orders for your customer if they have any.
Addresses Page
The last page you’ll create in the profile is the Addresses page which will allow the customer to see their shipping addresses.
Create the file
with the following content:Copy to clipboardpages/customer/addresses.js
You use the
field in theCopy to clipboardshipping_addresses
object stored in the context and display the addresses one after the other. You can also access the billing address if the customer has any usingCopy to clipboardcustomer
.Copy to clipboardcustomer.billing_address
If you go to the Addresses page now, you’ll see the customer’s shipping addresses listed.
What’s Next
Using the Medusa JS Client, you can easily interact with the APIs to create an even better customer experience in your storefront.
This tutorial did not cover all the aspects of a profile for simplicity. Here’s what else you can implement:
- View Single Orders: You can use the orders object you already retrieved on the orders page to show the information of a single order or you can use the Retrieve Order endpoint which is accessible in the client under
.Copy to clipboardclient.orders.retrieve
- Address Management: You can use the Update Shipping Address endpoint accessible in the client under
; you can use the Add Shipping Address endpoint which is accessible in the client underCopy to clipboardclient.customers.addresses.updateAddress
; and you can use the Delete Shipping Address endpoint which is accessible in the client underCopy to clipboardclient.customers.addresses.addAddress
.Copy to clipboardclient.customers.addresses.deleteAddress
- Saved Payment Methods: You can retrieve the saved payment methods if you use a payment provider that supports saving payment methods like Stripe using the Retrieve Saved Payment Methods endpoint accessible in the client under
.Copy to clipboardclient.customers.paymentMethods.list
- Reset Password: You can add reset password functionality using the Create Reset Password Token endpoint accessible in the client under
, then you can reset the password using the Reset Customer Password endpoint accessible in the client underCopy to clipboardclient.customers.generatePasswordToken
.Copy to clipboardclient.customers.resetPassword
You can check out the API documentation for a full list of endpoints to see what more functionalities you can add to your storefront.
Should you have any issues or questions related to Medusa, then feel free to reach out to the Medusa team via Discord.
Share this post
Try Medusa
Spin up your environment in a few minutes.

You may also like
On this page