Skip to content

Commit

Permalink
Merge pull request #19 from ReflectionsProjections/dev/aydan/cors
Browse files Browse the repository at this point in the history
Fixed CORS potentially?
  • Loading branch information
divyack2 authored Apr 14, 2024
2 parents 6ada1f8 + 70a62e5 commit 5da0b04
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
},
"dependencies": {
"@paralleldrive/cuid2": "^2.2.2",
"@types/cors": "^2.8.17",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.1",
"http-status-codes": "^2.3.0",
Expand Down
26 changes: 26 additions & 0 deletions src/middleware/cors-middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import cors from "cors";

// Allow CORS for Netlify deploy previews
const allowedOrigins = ["https://reflectionsprojections.org"];

// Function to check if the origin matches the deploy preview format
function isNetlifyDeployPreview(origin: string) {
const regex = /^https:\/\/deploy-preview-\d+--rp2024\.netlify\.app\/$/;
return regex.test(origin);
}

const corsMiddleware = cors({
origin: function (origin, callback) {
if (
!origin ||
allowedOrigins.includes(origin) ||
isNetlifyDeployPreview(origin)
) {
callback(null, true);
} else {
callback(new Error("Not allowed by CORS"));
}
},
});

export default corsMiddleware;
3 changes: 2 additions & 1 deletion src/services/subscription/subscription-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { Router } from "express";
import { StatusCodes } from "http-status-codes";
import { SubscriptionValidator } from "./subscription-schema";
import { Database } from "../../database";
import corsMiddleware from "../../middleware/cors-middleware";

const subscriptionRouter = Router();

// Create a new subscription
subscriptionRouter.post("/", async (req, res, next) => {
subscriptionRouter.post("/", corsMiddleware, async (req, res, next) => {
try {
// Validate the incoming user subscription
const subscriptionData = SubscriptionValidator.parse(req.body);
Expand Down
19 changes: 17 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,13 @@
dependencies:
"@types/node" "*"

"@types/cors@^2.8.17":
version "2.8.17"
resolved "https://registry.yarnpkg.com/@types/cors/-/cors-2.8.17.tgz#5d718a5e494a8166f569d986794e49c48b216b2b"
integrity sha512-8CGDvrBj1zgo2qE+oS3pOCyYNqCPryMWY2bGfwA0dcfopWGgxs+78df0Rs3rc9THP4JkOhLsAa+15VdpAqkcUA==
dependencies:
"@types/node" "*"

"@types/dotenv@^8.2.0":
version "8.2.0"
resolved "https://registry.npmjs.org/@types/dotenv/-/dotenv-8.2.0.tgz"
Expand Down Expand Up @@ -807,6 +814,14 @@ core-js-pure@^3.30.2:
resolved "https://registry.yarnpkg.com/core-js-pure/-/core-js-pure-3.36.1.tgz#1461c89e76116528b54eba20a0aff30164087a94"
integrity sha512-NXCvHvSVYSrewP0L5OhltzXeWFJLo2AL2TYnj6iLV3Bw8mM62wAQMNgUCRI6EBu6hVVpbCxmOPlxh1Ikw2PfUA==

cors@^2.8.5:
version "2.8.5"
resolved "https://registry.yarnpkg.com/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29"
integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==
dependencies:
object-assign "^4"
vary "^1"

cross-spawn@^7.0.2:
version "7.0.3"
resolved "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz"
Expand Down Expand Up @@ -2237,7 +2252,7 @@ [email protected]:
resolved "https://registry.yarnpkg.com/oauth/-/oauth-0.10.0.tgz#3551c4c9b95c53ea437e1e21e46b649482339c58"
integrity sha512-1orQ9MT1vHFGQxhuy7E/0gECD3fd2fCC+PIX+/jgmU/gI3EpRocXtmtvxCO5x3WZ443FLTLFWNDjl5MPJf9u+Q==

object-assign@^4.1.1:
object-assign@^4, object-assign@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
integrity sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==
Expand Down Expand Up @@ -2996,7 +3011,7 @@ v8-compile-cache@^2.0.3:
resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.4.0.tgz#cdada8bec61e15865f05d097c5f4fd30e94dc128"
integrity sha512-ocyWc3bAHBB/guyqJQVI5o4BZkPhznPYUG2ea80Gond/BgNWpap8TOmLSeeQG7bnh2KMISxskdADG59j7zruhw==

vary@~1.1.2:
vary@^1, vary@~1.1.2:
version "1.1.2"
resolved "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz"
integrity sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==
Expand Down

0 comments on commit 5da0b04

Please sign in to comment.