The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.
TypeScript b2b-marketplacecomposable-commerceecommerceenterpriseheadless-commercemarketplacemedusamedusajsmulti-vendortypescript
feat(core-plugin): auto-scan vendor routes for AUTHENTICATE=false to skip auth middleware
vholik93446291cded
diff --git a/packages/core-plugin/src/api/utils/index.ts b/packages/core-plugin/src/api/utils/index.tsindex c227fd77c..85a108990 100644--- a/packages/core-plugin/src/api/utils/index.ts+++ b/packages/core-plugin/src/api/utils/index.ts@@ -1 +1,2 @@-export * from "./unless-base-url"\ No newline at end of file+export * from "./unless-base-url"+export * from "./scan-unauthenticated-routes"\ No newline at end of filediff --git a/packages/core-plugin/src/api/utils/scan-unauthenticated-routes.ts b/packages/core-plugin/src/api/utils/scan-unauthenticated-routes.tsnew file mode 100644index 000000000..9a2fd3365--- /dev/null+++ b/packages/core-plugin/src/api/utils/scan-unauthenticated-routes.ts@@ -0,0 +1,72 @@+import fs from "fs"+import path from "path"++const VALID_EXTENSIONS = [".ts", ".tsx", ".js", ".jsx"]++function crawlRoutes(dir: string): string[] {+ const files: string[] = []++ if (!fs.existsSync(dir)) {+ return files+ }++ const entries = fs.readdirSync(dir, { withFileTypes: true })++ for (const entry of entries) {+ const fullPath = path.join(dir, entry.name)++ if (entry.isDirectory()) {+ files.push(...crawlRoutes(fullPath))+ } else if (entry.isFile()) {+ const ext = path.extname(entry.name)+ const baseName = path.basename(entry.name, ext)++ if (baseName === "route" && VALID_EXTENSIONS.includes(ext)) {+ files.push(fullPath)+ }+ }+ }++ return files+}++function hasAuthenticateFalse(filePath: string): boolean {+ try {+ const content = fs.readFileSync(filePath, "utf-8")+ return (+ /export\s+const\s+AUTHENTICATE\s*=\s*false/.test(content) ||+ /export\s*\{[^}]*\bAUTHENTICATE\b[^}]*\}/.test(content)+ )+ } catch {+ return false+ }+}++function filePathToRegex(filePath: string, apiDir: string): RegExp {+ const relativePath = path.relative(apiDir, filePath).replace(/\\/g, "/")+ const urlPath = relativePath.replace(/\/route\.(ts|tsx|js|jsx)$/, "")++ const segments = urlPath.split("/")+ const regexSegments = segments.map((segment) => {+ if (/^\[.+\]$/.test(segment)) {+ return "[^/]+"+ }+ return segment+ })++ return new RegExp("^\\/" + regexSegments.join("\\/") + "$")+}++export function scanUnauthenticatedRoutes(vendorDir: string): RegExp[] {+ const apiDir = path.dirname(vendorDir)+ const routeFiles = crawlRoutes(vendorDir)+ const patterns: RegExp[] = []++ for (const file of routeFiles) {+ if (hasAuthenticateFalse(file)) {+ patterns.push(filePathToRegex(file, apiDir))+ }+ }++ return patterns+}diff --git a/packages/core-plugin/src/api/utils/unless-base-url.ts b/packages/core-plugin/src/api/utils/unless-base-url.tsindex 3967e00c1..a12998372 100644--- a/packages/core-plugin/src/api/utils/unless-base-url.ts+++ b/packages/core-plugin/src/api/utils/unless-base-url.ts@@ -4,15 +4,17 @@ import { MedusaRequest } from '@medusajs/framework'/*** Due to Medusa's `unlessPath` function bug, we need to use this function to skip middlewares for particular routes.- * @param onPath - Regular expression to match against the base URL+ * @param onPath - Regular expression or array of regular expressions to match against the base URL* @param middleware - The middleware function to execute* @param methods - Optional array of HTTP methods to match against. If not provided, matches all methods.*/export const unlessBaseUrl =- (onPath: RegExp, middleware: MiddlewareFunction, methods?: string[]) =>+ (onPath: RegExp | RegExp[], middleware: MiddlewareFunction, methods?: string[]) =>(req: MedusaRequest, res: MedusaResponse, next: MedusaNextFunction) => {const methodMatches = !methods || methods.includes(req.method.toUpperCase())- if (onPath.test(req.baseUrl) && methodMatches) {+ const paths = Array.isArray(onPath) ? onPath : [onPath]+ const pathMatches = paths.some(p => p.test(req.baseUrl))+ if (pathMatches && methodMatches) {return next()} else {return middleware(req, res, next)diff --git a/packages/core-plugin/src/api/vendor/middlewares.ts b/packages/core-plugin/src/api/vendor/middlewares.tsindex 35c2e37b0..c52db4f9c 100644--- a/packages/core-plugin/src/api/vendor/middlewares.ts+++ b/packages/core-plugin/src/api/vendor/middlewares.ts@@ -39,9 +39,13 @@ import { vendorShippingProfilesMiddlewares } from "./shipping-profiles/middlewarimport { vendorStockLocationsMiddlewares } from "./stock-locations/middlewares"import { vendorStoresMiddlewares } from "./stores/middlewares"import { vendorUploadsMiddlewares } from "./uploads/middlewares"-import { unlessBaseUrl } from "../utils"+import { scanUnauthenticatedRoutes, unlessBaseUrl } from "../utils"import { vendorProductTagsMiddlewares } from "./product-tags/middlewares"+const unauthenticatedRoutes = [+ /^\/vendor\/sellers$/,+ ...scanUnauthenticatedRoutes(__dirname),+]export const vendorMiddlewares: MiddlewareRoute[] = [{@@ -70,7 +74,7 @@ export const vendorMiddlewares: MiddlewareRoute[] = [matcher: "/vendor/*",middlewares: [unlessBaseUrl(- /^\/vendor\/sellers$/,+ unauthenticatedRoutes,authenticate("seller", ["bearer", "session"], {allowUnregistered: false,})