Skip to content

Commit ca09cca

Browse files
authored
Merge pull request #103 from MeshJS/API
add more logging and cors to add tx.
2 parents cd4518c + 59f762c commit ca09cca

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

src/lib/cors.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
1-
import Cors from 'cors';
2-
import initMiddleware from './init-middleware';
1+
import Cors from "cors";
2+
import initMiddleware from "./init-middleware";
33

4-
const rawOrigins = process.env.CORS_ORIGINS || '';
5-
const allowedOrigins = rawOrigins === '*' ? '*' : rawOrigins.split(',').map(o => o.trim());
4+
const rawOrigins = process.env.CORS_ORIGINS || "";
5+
const allowedOrigins =
6+
rawOrigins === "*" ? "*" : rawOrigins.split(",").map((o) => o.trim());
67

78
export const cors = initMiddleware(
89
Cors({
9-
methods: ['GET', 'POST', 'OPTIONS'],
10+
methods: ["GET", "POST", "OPTIONS"],
11+
allowedHeaders: ["Content-Type", "Authorization"],
1012
credentials: true,
1113
origin: function (
1214
origin: string | undefined,
13-
callback: (err: Error | null, allow?: boolean) => void
15+
callback: (err: Error | null, allow?: boolean) => void,
1416
) {
15-
if (!origin) return callback(null, true);
16-
if (allowedOrigins === '*') return callback(null, true);
17+
console.log("---- CORS DEBUG ----");
18+
console.log("Request origin:", origin);
19+
console.log("Allowed origins:", allowedOrigins);
20+
21+
if (!origin) {
22+
console.log("No origin provided. Allowing by default.");
23+
return callback(null, true);
24+
}
25+
if (allowedOrigins === "*") {
26+
console.log("Wildcard origin match. Allowing all.");
27+
return callback(null, true);
28+
}
1729
if (allowedOrigins.includes(origin)) {
30+
console.log("Origin allowed.");
1831
return callback(null, true);
1932
} else {
33+
console.error(`Origin ${origin} not allowed by CORS`);
2034
return callback(new Error(`Origin ${origin} not allowed by CORS`));
2135
}
22-
}
23-
})
24-
);
36+
},
37+
}),
38+
);

src/pages/api/v1/addTransaction.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import type { NextApiRequest, NextApiResponse } from "next";
22
import { db } from "@/server/db";
33
import { verifyJwt } from "@/lib/verifyJwt";
4+
import { cors } from "@/lib/cors";
45

56
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
7+
8+
await cors(req, res);
9+
if (req.method === "OPTIONS") {
10+
return res.status(200).end();
11+
}
12+
613
if (req.method !== "POST") {
714
return res.status(405).json({ error: "Method Not Allowed" });
815
}

0 commit comments

Comments
 (0)