forked from StreamFi-x/streamfi-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_middleware.ts
More file actions
25 lines (20 loc) · 728 Bytes
/
_middleware.ts
File metadata and controls
25 lines (20 loc) · 728 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { NextRequest, NextResponse } from "next/server";
export function middleware(req: NextRequest) {
// Skipping CSRF check for GET requests
if (req.method === "GET") {
return NextResponse.next();
}
// Check CSRF token for POST requests, this is to give access to post request and prevent attacker
const csrfToken = req.headers.get("x-csrf-token");
const expectedToken = process.env.CSRF_SECRET;
if (!csrfToken || csrfToken !== expectedToken) {
return new Response(JSON.stringify({ error: "Invalid CSRF token" }), {
status: 403,
headers: { "Content-Type": "application/json" },
});
}
return NextResponse.next();
}
export const config = {
matcher: "/api/newsletter/:path*",
};