#Unauthorized when creating a draft order on storefront

1 messages · Page 1 of 1 (latest)

glass flame
#

What do I need to create a draft order on the storefront with the medusa client?

rose heartBOT
unreal compass
#

Creating a draft order is a part of Admin API. You need to first authenticate as an admin. You can use medusa.admin.AdminAuthResource.createSession(...) with admin account data, then you should be able to create draft order.

glass flame
# unreal compass Creating a draft order is a part of Admin API. You need to first authenticate as...

Maybe I'm not sending the right headers to admin.draftOrders.create

export async function createDraftOrder(payload) {
  const headers = getMedusaHeaders(["cart"])

  await medusaClient.admin.auth.createSession(
    {
      email: "[email protected]",
      password: "supersecret",
    },
    headers
  )

  return medusaClient.admin.draftOrders
    .create(payload, headers)
    .then(({ draft_order }) => draft_order)
    .catch((err) => medusaError(err))
}
unreal compass
#

It is not headers fault. You can even remove headers as this function require only customHeaders. By default is empty object and that is fine. Your code should work without headers.

You can check two things:

  • Are your credentials ok? Does this account really exist?
  • After running the function, check on the network whether CORS is blocking your authorization and what status you have received. Since you want to use the Admin API, you need to add the application URL in ADMIN_CORS.
glass flame
#

It only works when I explicit pass the connect.sid cookie to the admin.draftOrders.create function