Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Refactor rate limit handling in auth modules

Kiran Kea1f17073ab9
diff --git a/apps/web/lib/auth/partner.ts b/apps/web/lib/auth/partner.ts
index 954589b582..2f21ef63ab 100644
--- a/apps/web/lib/auth/partner.ts
+++ b/apps/web/lib/auth/partner.ts
@@ -89,11 +89,13 @@ export const withPartnerProfile = (
identifier: `partner-profile:ratelimit:${session.user.id}`,
});
- if (!success) {
+ if (headers) {
for (const [key, value] of Object.entries(headers)) {
responseHeaders.set(key, value);
}
+ }
+ if (!success) {
throw new DubApiError({
code: "rate_limit_exceeded",
message: "Too many requests.",
diff --git a/apps/web/lib/auth/rate-limit-request.ts b/apps/web/lib/auth/rate-limit-request.ts
index 3a3e3b27d6..2b909280ce 100644
--- a/apps/web/lib/auth/rate-limit-request.ts
+++ b/apps/web/lib/auth/rate-limit-request.ts
@@ -14,15 +14,8 @@ export async function rateLimitRequest({
interval,
).limit(identifier);
- if (success) {
- return {
- success: true,
- headers: {},
- };
- }
-
return {
- success: false,
+ success,
headers: {
"Retry-After": reset.toString(),
"X-RateLimit-Limit": limit.toString(),
diff --git a/apps/web/lib/auth/workspace.ts b/apps/web/lib/auth/workspace.ts
index ef2f933ebb..54f45e895e 100644
--- a/apps/web/lib/auth/workspace.ts
+++ b/apps/web/lib/auth/workspace.ts
@@ -247,11 +247,13 @@ export const withWorkspace = (
interval,
});
- if (!success) {
+ if (headers) {
for (const [key, value] of Object.entries(headers)) {
responseHeaders.set(key, value);
}
+ }
+ if (!success) {
throw new DubApiError({
code: "rate_limit_exceeded",
message: "Too many requests.",
@@ -321,11 +323,11 @@ export const withWorkspace = (
interval: rateLimit.interval,
});
- if (!success) {
- for (const [key, value] of Object.entries(headers)) {
- responseHeaders.set(key, value);
- }
+ for (const [key, value] of Object.entries(headers)) {
+ responseHeaders.set(key, value);
+ }
+ if (!success) {
throw new DubApiError({
code: "rate_limit_exceeded",
message: "Too many requests.",