Skip to content

Commit 246313b

Browse files
authored
Pav 51 fix api connection issue (#130)
2 parents 3e22e93 + a9386eb commit 246313b

3 files changed

Lines changed: 16 additions & 9 deletions

File tree

backend/src/middleware/cors.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import cors from "cors";
22

3+
// Regex cobre qualquer preview do Vercel: painel-vagas-*.vercel.app
4+
// (hash-based e branch-based, ex: git-master-bene-teslas-projects)
5+
const VERCEL_PREVIEW_RE = /^https:\/\/painel-vagas-[a-z0-9-]+\.vercel\.app$/;
6+
37
const DEFAULT_ALLOWED_ORIGINS = [
48
"https://painel-vagas-lake.vercel.app",
9+
"https://painel-vagas-git-master-bene-teslas-projects.vercel.app",
510
"https://painel-vagas-m6hbzlqeh-bene-teslas-projects.vercel.app",
611
"https://jobsglobalscraper.ddns.net",
7-
"http://jobsglobalscraper.ddns.net",
812
"http://localhost:5173",
913
"http://localhost:5174",
1014
];
@@ -19,14 +23,17 @@ function parseAllowedOrigins(value: string | undefined): Set<string> {
1923

2024
export const corsOptions: cors.CorsOptions = {
2125
origin(origin, callback) {
22-
const allowedOrigins = parseAllowedOrigins(
23-
process.env.CORS_ALLOWED_ORIGINS,
24-
);
25-
if (!origin || allowedOrigins.has(origin)) return callback(null, true);
26+
if (!origin) return callback(null, true);
27+
28+
if (VERCEL_PREVIEW_RE.test(origin)) return callback(null, true);
29+
30+
const allowedOrigins = parseAllowedOrigins(process.env.CORS_ALLOWED_ORIGINS);
31+
if (allowedOrigins.has(origin)) return callback(null, true);
32+
2633
callback(new Error("Origin not allowed by CORS"));
2734
},
28-
methods: ["GET", "POST", "OPTIONS"],
29-
allowedHeaders: ["Content-Type", "Authorization"],
35+
methods: ["GET", "POST", "PATCH", "DELETE", "OPTIONS"],
36+
allowedHeaders: ["Content-Type", "Authorization", "X-Requested-With"],
3037
credentials: true,
3138
maxAge: 86400,
3239
};

frontend/vercel.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"rewrites": [
33
{
44
"source": "/api/(.*)",
5-
"destination": "http://jobsglobalscraper.ddns.net/api/$1"
5+
"destination": "https://jobsglobalscraper.ddns.net/api/$1"
66
},
77
{
88
"source": "/(.*)",

vercel.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"rewrites": [
33
{
44
"source": "/api/(.*)",
5-
"destination": "http://jobsglobalscraper.ddns.net/api/$1"
5+
"destination": "https://jobsglobalscraper.ddns.net/api/$1"
66
},
77
{
88
"source": "/((?!api/).*)",

0 commit comments

Comments
 (0)