Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

fix(core): require member auth on GET /vendor/stores (#1450)

Viktor Holikde3627a5c5f3
diff --git a/integration-tests/http/store-info/vendor/stores.spec.ts b/integration-tests/http/store-info/vendor/stores.spec.ts
new file mode 100644
index 000000000..8e553a814
--- /dev/null
+++ b/integration-tests/http/store-info/vendor/stores.spec.ts
@@ -0,0 +1,44 @@
+import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
+import { MedusaContainer } from "@medusajs/framework/types"
+import { createSellerDefaultsWorkflow } from "@mercurjs/core/workflows"
+import { createSellerUser } from "../../../helpers/create-seller-user"
+
+jest.setTimeout(50000)
+
+medusaIntegrationTestRunner({
+ testSuite: ({ getContainer, api }) => {
+ describe("Vendor - Stores", () => {
+ let appContainer: MedusaContainer
+ let headers: any
+
+ beforeAll(async () => {
+ appContainer = getContainer()
+ })
+
+ beforeEach(async () => {
+ await createSellerDefaultsWorkflow(appContainer).run()
+
+ const result = await createSellerUser(appContainer, {
+ email: "[email protected]",
+ name: "Seller Store",
+ })
+ headers = result.headers
+ })
+
+ it("rejects anonymous requests", async () => {
+ const error = await api
+ .get("/vendor/stores")
+ .catch((e) => e)
+
+ expect(error.response.status).toBe(401)
+ })
+
+ it("returns stores for an authenticated member", async () => {
+ const response = await api.get("/vendor/stores", headers)
+
+ expect(response.status).toBe(200)
+ expect(Array.isArray(response.data.stores)).toBe(true)
+ })
+ })
+ },
+})
diff --git a/packages/core/src/api/vendor/middlewares.ts b/packages/core/src/api/vendor/middlewares.ts
index 87dcf9fba..1cbbc7176 100644
--- a/packages/core/src/api/vendor/middlewares.ts
+++ b/packages/core/src/api/vendor/middlewares.ts
@@ -49,11 +49,14 @@ const unauthenticatedRoutes = [
/^\/vendor\/sellers$/,
/^\/vendor\/sellers\/select$/,
/^\/vendor\/feature-flags$/,
- /^\/vendor\/stores$/,
/^\/vendor\/members\/invites\/accept$/,
...scanUnauthenticatedRoutes(process.cwd()),
]
+// Marketplace-level routes: authenticated, but reachable before the member
+// belongs to a seller (onboarding).
+const sellerlessRoutes = [...unauthenticatedRoutes, /^\/vendor\/stores$/]
+
export const vendorMiddlewares: MiddlewareRoute[] = [
{
matcher: "/vendor/sellers",
@@ -84,7 +87,7 @@ export const vendorMiddlewares: MiddlewareRoute[] = [
})
),
unlessBaseUrl(
- unauthenticatedRoutes,
+ sellerlessRoutes,
ensureSellerMiddleware
),
],
diff --git a/packages/core/src/api/vendor/stores/route.ts b/packages/core/src/api/vendor/stores/route.ts
index aa74bf65d..15e10348a 100644
--- a/packages/core/src/api/vendor/stores/route.ts
+++ b/packages/core/src/api/vendor/stores/route.ts
@@ -2,13 +2,22 @@ import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "@medusajs/framework"
-import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
+import { ContainerRegistrationKeys, MedusaError } from "@medusajs/framework/utils"
import { HttpTypes } from "@medusajs/framework/types"
export const GET = async (
req: AuthenticatedMedusaRequest<HttpTypes.AdminStoreListParams>,
res: MedusaResponse<HttpTypes.AdminStoreListResponse>
) => {
+ const memberId = req.auth_context?.actor_id
+
+ if (!memberId) {
+ throw new MedusaError(
+ MedusaError.Types.UNAUTHORIZED,
+ "You must be authenticated to access store information."
+ )
+ }
+
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)
const { data: stores, metadata } = await query.graph({