Easy way to add rate limiter into t3 stack / trpc?

Is there any recommended way to add a rate limiter into a t3 stack project / trpc?
6 Replies
Kev
Kev2y ago
https://github.com/upstash/ratelimit has been recommended before
GitHub
GitHub - upstash/ratelimit: Rate limiting library for serverless ru...
Rate limiting library for serverless runtimes. Contribute to upstash/ratelimit development by creating an account on GitHub.
romulus_117
romulus_1172y ago
https://youtu.be/YkOSUVzOAA4 Around the 1:38:01 mark
Theo - t3․gg
YouTube
T3 Stack Tutorial - FROM 0 TO PROD FOR $0 (Next.js, tRPC, TypeScrip...
I've never worked this hard on a video before. I really hope y'all can benefit from this 🙏 GITHUB REPO https://github.com/t3dotgg/chirp DEPLOYED APP https://xn--uo8h.t3.gg/ GET A JACKET IF YOU'RE COOL LIKE THAT https://shop.t3.gg/ ALL MY VIDEOS ARE POSTED EARLY ON PATREON https://www.patreon.com/t3dotgg Everything else (Twitch, Twitter, Discor...
rovrav
rovravOP2y ago
Is this a better solution to lodash debounce?
Neto
Neto2y ago
lodash is a lib to delay execution a web rate limiter is to stop too many sequential requests from the same actor
rovrav
rovravOP17mo ago
Can you do a global rate limiter, this video only shows you do it from from an individual route
export async function middleware(req: NextRequest) {
// We need to create a response and hand it to the supabase client to be able to modify the response headers.
const res = NextResponse.next();
// Create authenticated Supabase Client.
const supabase = createMiddlewareClient({ req, res });

// Rate limit by IP address
let ipAddress;

if (req && req?.ip) {
ipAddress = req.ip;
}

let success;

if (ipAddress) {
const rateLimitResult = await ratelimit.limit(ipAddress);
success = rateLimitResult.success;
}

// If rate limit is exceeded, respond with a 429 status code
if (!success) {
return NextResponse.error({ status: 429, statusText: "Too Many Requests" });
}

// Check if we have a session
const {
data: { session },
} = await supabase.auth.getSession();

// Check if user is on the signin page
if (req.nextUrl.pathname === "/signin" || req.nextUrl.pathname === "/") {
return res;
}

// Check auth condition
if (session) {
// Authentication successful, forward request to protected route.
return res;
}

// Auth condition not met, redirect to home page.
const redirectUrl = req.nextUrl.clone();
redirectUrl.pathname = "/signin";
redirectUrl.searchParams.set(`redirectedFrom`, req.nextUrl.pathname);
return NextResponse.redirect(redirectUrl);
}
export async function middleware(req: NextRequest) {
// We need to create a response and hand it to the supabase client to be able to modify the response headers.
const res = NextResponse.next();
// Create authenticated Supabase Client.
const supabase = createMiddlewareClient({ req, res });

// Rate limit by IP address
let ipAddress;

if (req && req?.ip) {
ipAddress = req.ip;
}

let success;

if (ipAddress) {
const rateLimitResult = await ratelimit.limit(ipAddress);
success = rateLimitResult.success;
}

// If rate limit is exceeded, respond with a 429 status code
if (!success) {
return NextResponse.error({ status: 429, statusText: "Too Many Requests" });
}

// Check if we have a session
const {
data: { session },
} = await supabase.auth.getSession();

// Check if user is on the signin page
if (req.nextUrl.pathname === "/signin" || req.nextUrl.pathname === "/") {
return res;
}

// Check auth condition
if (session) {
// Authentication successful, forward request to protected route.
return res;
}

// Auth condition not met, redirect to home page.
const redirectUrl = req.nextUrl.clone();
redirectUrl.pathname = "/signin";
redirectUrl.searchParams.set(`redirectedFrom`, req.nextUrl.pathname);
return NextResponse.redirect(redirectUrl);
}
I'm trying to add a rate limiter to the middleware function, is this the best way? I'm getting errors with this
romulus_117
romulus_11717mo ago
https://upstash.com/blog/edge-rate-limiting Could try something like this
Rate Limiting Your Next.js App with Vercel Edge
Articles and tutorials on serverless technologies from Upstash and community
Want results from more Discord servers?
Add your server