Skip to content

Commit

Permalink
Merge pull request #64 from divyansh355/main
Browse files Browse the repository at this point in the history
Enhance CORS Configuration for Production Security
  • Loading branch information
ghoshRitesh12 authored Oct 4, 2024
2 parents dbbd46a + 91fd091 commit 3813902
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
DOMAIN = "aniwatchtv.to"
PORT = 4000
CORS_ALLOWED_ORIGINS = https://your-production-domain.com,https://another-trusted-domain.com

# RATE LIMIT
WINDOWMS = 1800000 # duration to track requests (in milliseconds) for rate limiting. here, 30*60*1000 = 1800000 = 30 minutes
Expand Down
21 changes: 18 additions & 3 deletions src/config/cors.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import cors from "cors";
import cors from 'cors';
import dotenv from 'dotenv';

dotenv.config();

const allowedOrigins = process.env.CORS_ALLOWED_ORIGINS
? process.env.CORS_ALLOWED_ORIGINS.split(",")
: ["http://localhost:4000"];

const corsConfig = cors({
origin: "*",
methods: "GET",
origin: function (origin, callback) {
if (!origin || allowedOrigins.includes(origin)) {
callback(null, true);
} else {
callback(new Error("Not allowed by CORS"));
}
},
methods: ["GET"],
credentials: true,
optionsSuccessStatus: 200,
maxAge: 600,
});

export default corsConfig;

0 comments on commit 3813902

Please sign in to comment.