|
@@ -1,16 +1,20 @@
|
|
|
-import { ConfigModule } from "@medusajs/medusa";
|
|
|
import { Router } from "express";
|
|
|
+import cors from "cors";
|
|
|
+import bodyParser from "body-parser";
|
|
|
+import { authenticate, ConfigModule } from "@medusajs/medusa";
|
|
|
import { getConfigFile } from "medusa-core-utils";
|
|
|
-import { getAdminRouter } from "./routes/admin";
|
|
|
-import { getStoreRouter } from "./routes/store";
|
|
|
+import { attachStoreRoutes } from "./routes/store";
|
|
|
+import { attachAdminRoutes } from "./routes/admin";
|
|
|
|
|
|
export default (rootDirectory: string): Router | Router[] => {
|
|
|
+ // Read currently-loaded medusa config
|
|
|
const { configModule } = getConfigFile<ConfigModule>(
|
|
|
rootDirectory,
|
|
|
"medusa-config"
|
|
|
);
|
|
|
const { projectConfig } = configModule;
|
|
|
|
|
|
+ // Set up our CORS options objects, based on config
|
|
|
const storeCorsOptions = {
|
|
|
origin: projectConfig.store_cors.split(","),
|
|
|
credentials: true,
|
|
@@ -21,8 +25,27 @@ export default (rootDirectory: string): Router | Router[] => {
|
|
|
credentials: true,
|
|
|
};
|
|
|
|
|
|
- const storeRouter = getStoreRouter(storeCorsOptions);
|
|
|
- const adminRouter = getAdminRouter(adminCorsOptions);
|
|
|
+ // Set up express router
|
|
|
+ const router = Router();
|
|
|
|
|
|
- return [storeRouter, adminRouter];
|
|
|
+ // Set up root routes for store and admin endpoints, with appropriate CORS settings
|
|
|
+ router.use("/store", cors(storeCorsOptions), bodyParser.json());
|
|
|
+ router.use("/admin", cors(adminCorsOptions), bodyParser.json());
|
|
|
+
|
|
|
+ // Add authentication to all admin routes *except* auth and account invite ones
|
|
|
+ router.use(/\/admin\/((?!auth)(?!invites).*)/, authenticate());
|
|
|
+
|
|
|
+ // Set up routers for store and admin endpoints
|
|
|
+ const storeRouter = Router();
|
|
|
+ const adminRouter = Router();
|
|
|
+
|
|
|
+ // Attach these routers to the root routes
|
|
|
+ router.use("/store", storeRouter);
|
|
|
+ router.use("/admin", adminRouter);
|
|
|
+
|
|
|
+ // Attach custom routes to these routers
|
|
|
+ attachStoreRoutes(storeRouter);
|
|
|
+ attachAdminRoutes(adminRouter);
|
|
|
+
|
|
|
+ return router;
|
|
|
};
|