This commit is contained in:
Atridad Lahiji 2023-07-03 16:21:00 -06:00
parent 3de5f3e7ba
commit 6fa39d63a3
No known key found for this signature in database
3 changed files with 27 additions and 27 deletions

View file

@ -21,7 +21,6 @@
"@trpc/next": "10.33.0",
"@trpc/react-query": "10.33.0",
"@trpc/server": "10.33.0",
"@upstash/ratelimit": "^0.4.3",
"@upstash/redis": "^1.22.0",
"ably": "^1.2.41",
"autoprefixer": "^10.4.14",

View file

@ -21,15 +21,6 @@ import { getServerAuthSession } from "~/server/auth";
import { prisma } from "~/server/db";
import { Redis } from "@upstash/redis";
const rateLimit = new Ratelimit({
redis: Redis.fromEnv(),
limiter: Ratelimit.slidingWindow(
Number(env.UPSTASH_RATELIMIT_REQUESTS),
`${Number(env.UPSTASH_RATELIMIT_SECONDS)}s`
),
analytics: true,
});
type CreateContextOptions = {
session: Session | null;
};
@ -114,16 +105,34 @@ const enforceRouteProtection = t.middleware(async ({ ctx, next }) => {
if (!ctx.session || !ctx.session.user) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
const { success } = await rateLimit.limit(
`${env.APP_ENV}_${ctx.session.user.id}`
);
if (!success) throw new TRPCError({ code: "TOO_MANY_REQUESTS" });
return next({
ctx: {
session: { ...ctx.session, user: ctx.session.user },
},
});
try {
const rateLimit = new Ratelimit({
redis: Redis.fromEnv(),
limiter: Ratelimit.slidingWindow(
Number(env.UPSTASH_RATELIMIT_REQUESTS),
`${Number(env.UPSTASH_RATELIMIT_SECONDS)}s`
),
analytics: true,
});
const { success } = await rateLimit.limit(
`${env.APP_ENV}_${ctx.session.user.id}`
);
if (!success) throw new TRPCError({ code: "TOO_MANY_REQUESTS" });
return next({
ctx: {
session: { ...ctx.session, user: ctx.session.user },
},
});
} catch {
return next({
ctx: {
session: { ...ctx.session, user: ctx.session.user },
},
});
}
});
/**

View file

@ -28,14 +28,6 @@ export const api = createTRPCNext<AppRouter>({
*/
transformer: superjson,
queryClientConfig: {
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
},
},
},
/**
* Links used to determine request flow from client to server.
*