|
1 | | -import { NextRequest } from "next/server"; |
| 1 | +import { NextRequest, NextResponse } from "next/server"; |
2 | 2 | import { getEnv } from "@/lib/env"; |
3 | 3 | import { exchangeCodeForToken, validateProject } from "@/lib/auth-utils"; |
4 | 4 | import { |
|
7 | 7 | getBaseUrl, |
8 | 8 | } from "@/lib/response-utils"; |
9 | 9 | import { transferProject } from "@/lib/project-transfer"; |
| 10 | +import { buildRateLimitKey } from "@/lib/server/ratelimit"; |
10 | 11 |
|
11 | 12 | async function sendServerAnalyticsEvent( |
12 | 13 | event: string, |
@@ -46,21 +47,25 @@ async function sendServerAnalyticsEvent( |
46 | 47 | export async function GET(request: NextRequest) { |
47 | 48 | try { |
48 | 49 | const env = getEnv(); |
49 | | - const { searchParams } = new URL(request.url); |
| 50 | + const url = new URL(request.url); |
| 51 | + const { searchParams } = url; |
50 | 52 |
|
51 | 53 | const code = searchParams.get("code"); |
52 | 54 | const state = searchParams.get("state"); |
53 | 55 | const projectID = searchParams.get("projectID"); |
54 | 56 |
|
55 | | - // Rate limiting |
56 | | - const rateLimitResult = await env.CLAIM_DB_RATE_LIMITER.limit({ |
57 | | - key: request.url, |
58 | | - }); |
59 | | - if (!rateLimitResult.success) { |
60 | | - return redirectToError( |
61 | | - request, |
62 | | - "Rate Limited", |
63 | | - "We're experiencing high demand. Please try again later." |
| 57 | + const key = buildRateLimitKey(request); |
| 58 | + |
| 59 | + // --- Simple rate limiting --- |
| 60 | + const { success } = await env.CLAIM_DB_RATE_LIMITER.limit({ key }); |
| 61 | + if (!success) { |
| 62 | + return NextResponse.json( |
| 63 | + { |
| 64 | + error: "rate_limited", |
| 65 | + message: "Rate limit exceeded. Please try again later.", |
| 66 | + path: url.pathname, |
| 67 | + }, |
| 68 | + { status: 429 } |
64 | 69 | ); |
65 | 70 | } |
66 | 71 |
|
|
0 commit comments