|
1 | 1 | import { NextRequest, NextResponse } from "next/server"; |
2 | 2 | import { checkRateLimit } from "../rate-limit"; |
3 | 3 |
|
4 | | -export default function RateLimiter(request: NextRequest) { |
5 | | - if (/api\//.test(request.nextUrl.pathname)) { |
6 | | - const forwardedFor = request.headers.get("x-forwarded-for"); |
7 | | - const ip = forwardedFor?.split(",")[0] || "unknown"; |
8 | | - const origin = request.headers.get("origin"); |
9 | | - |
10 | | - const allowedOrigins = |
11 | | - process.env.NODE_ENV === "development" |
12 | | - ? [/^http:\/\/localhost:\d+$/, /^http:\/\/127\.0\.0\.1:\d+$/] |
13 | | - : [/^https?:\/\/devpulse-waka\.vercel\.app(:\d+)?$/]; |
14 | | - |
15 | | - const isAllowed1 = |
16 | | - !origin || allowedOrigins.some((pattern) => pattern.test(origin)); |
17 | | - |
18 | | - if (!isAllowed1) { |
19 | | - return NextResponse.json( |
20 | | - { error: "Hehe you're going too far naah..." }, |
21 | | - { status: 403 }, |
22 | | - ); |
23 | | - } |
24 | | - |
25 | | - const maxRequest = /api\/(login|signup)/.test(request.nextUrl.pathname) |
26 | | - ? 5 |
27 | | - : 10; |
28 | | - |
29 | | - const window = /api\/(login|signup)/.test(request.nextUrl.pathname) |
30 | | - ? 60 * 60 * 1000 |
31 | | - : 5 * 60 * 1000; |
32 | | - |
33 | | - const isAllowed = checkRateLimit(ip, maxRequest, window); |
34 | | - |
35 | | - if (!isAllowed) { |
36 | | - return NextResponse.json({ error: "Too many requests" }, { status: 429 }); |
37 | | - } |
| 4 | +export default function RateLimiter( |
| 5 | + request: NextRequest, |
| 6 | +): NextResponse | undefined { |
| 7 | + if (!/api\//.test(request.nextUrl.pathname)) { |
| 8 | + return undefined; |
38 | 9 | } |
39 | 10 |
|
40 | | - return NextResponse.next(); |
| 11 | + const forwardedFor = request.headers.get("x-forwarded-for"); |
| 12 | + const ip = forwardedFor?.split(",")[0]?.trim() || "unknown"; |
| 13 | + const origin = request.headers.get("origin"); |
| 14 | + |
| 15 | + const allowedOrigins = |
| 16 | + process.env.NODE_ENV === "development" |
| 17 | + ? [/^http:\/\/localhost:\d+$/, /^http:\/\/127\.0\.0\.1:\d+$/] |
| 18 | + : [/^https?:\/\/devpulse-waka\.vercel\.app(:\d+)?$/]; |
| 19 | + |
| 20 | + const isOriginAllowed = |
| 21 | + !origin || allowedOrigins.some((pattern) => pattern.test(origin)); |
| 22 | + |
| 23 | + if (!isOriginAllowed) { |
| 24 | + return NextResponse.json( |
| 25 | + { error: "Hehe you're going too far naah..." }, |
| 26 | + { status: 403 }, |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + const isAuthEndpoint = /api\/(login|signup)/.test(request.nextUrl.pathname); |
| 31 | + const maxRequests = isAuthEndpoint ? 5 : 10; |
| 32 | + const windowMs = isAuthEndpoint ? 60 * 60 * 1000 : 5 * 60 * 1000; |
| 33 | + |
| 34 | + const withinLimit = checkRateLimit(ip, maxRequests, windowMs); |
| 35 | + |
| 36 | + if (!withinLimit) { |
| 37 | + return NextResponse.json({ error: "Too many requests" }, { status: 429 }); |
| 38 | + } |
| 39 | + |
| 40 | + // let it through so auth still runs |
| 41 | + return undefined; |
41 | 42 | } |
0 commit comments