mirror of
https://github.com/lukevella/rallly.git
synced 2025-08-03 08:28:35 +02:00
🔒️ Rate limit by ip address (#1155)
This commit is contained in:
parent
491af5c71b
commit
f7eda38e0a
6 changed files with 32 additions and 12 deletions
|
@ -38,6 +38,7 @@
|
|||
"@trpc/react-query": "^10.13.0",
|
||||
"@upstash/ratelimit": "^1.2.1",
|
||||
"@vercel/kv": "^2.0.0",
|
||||
"request-ip": "^3.3.0",
|
||||
"@vercel/functions": "^1.0.2",
|
||||
"accept-language-parser": "^1.5.0",
|
||||
"autoprefixer": "^10.4.13",
|
||||
|
@ -83,6 +84,7 @@
|
|||
"@rallly/eslint-config": "*",
|
||||
"@types/accept-language-parser": "^1.5.3",
|
||||
"@types/color-hash": "^1.0.2",
|
||||
"@types/request-ip": "^0.0.41",
|
||||
"@types/lodash": "^4.14.178",
|
||||
"@types/react-big-calendar": "^1.8.8",
|
||||
"@types/smoothscroll-polyfill": "^0.3.1",
|
||||
|
|
|
@ -4,6 +4,7 @@ import * as Sentry from "@sentry/nextjs";
|
|||
import { createNextApiHandler } from "@trpc/server/adapters/next";
|
||||
import { Ratelimit } from "@upstash/ratelimit";
|
||||
import { kv } from "@vercel/kv";
|
||||
import requestIp from "request-ip";
|
||||
|
||||
import { posthog, posthogApiHandler } from "@/app/posthog";
|
||||
import { absoluteUrl, shortUrl } from "@/utils/absolute-url";
|
||||
|
@ -46,11 +47,18 @@ const trpcApiHandler = createNextApiHandler<AppRouter>({
|
|||
isEmailBlocked,
|
||||
absoluteUrl,
|
||||
shortUrl,
|
||||
ratelimit: async (key: string) => {
|
||||
ratelimit: async () => {
|
||||
if (!process.env.KV_REST_API_URL) {
|
||||
return { success: true };
|
||||
}
|
||||
return ratelimit.limit(key);
|
||||
|
||||
const clientIp = requestIp.getClientIp(opts.req);
|
||||
|
||||
if (!clientIp) {
|
||||
return { success: false };
|
||||
}
|
||||
|
||||
return ratelimit.limit(clientIp);
|
||||
},
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue