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.

rearrange

Kiran K8a8c4b2d8f64
diff --git a/apps/web/lib/referrals/auth.ts b/apps/web/lib/referrals/auth.ts
index 63c8924bae..cebd5ac7fc 100644
--- a/apps/web/lib/referrals/auth.ts
+++ b/apps/web/lib/referrals/auth.ts
@@ -31,7 +31,7 @@ export const withAuth = (handler: WithAuthHandler) => {
try {
let link: Link | undefined = undefined;
- let publicToken: string | undefined = undefined;
+ let tokenFromHeader: string | undefined = undefined;
const rateLimit = 100;
const searchParams = getSearchParams(req.url);
@@ -52,58 +52,39 @@ export const withAuth = (handler: WithAuthHandler) => {
});
}
- publicToken = authorizationHeader.replace("Bearer ", "");
+ tokenFromHeader = authorizationHeader.replace("Bearer ", "");
- if (!publicToken) {
+ if (!tokenFromHeader) {
throw new DubApiError({
code: "unauthorized",
message: "Missing Authorization header.",
});
}
- const referralToken = await prisma.referralPublicToken.findUnique({
+ const publicToken = await prisma.referralPublicToken.findUnique({
where: {
- publicToken,
+ publicToken: tokenFromHeader,
},
});
- if (!referralToken) {
+ if (!publicToken) {
throw new DubApiError({
code: "unauthorized",
message: "Invalid public token.",
});
}
- if (referralToken.expires < new Date()) {
+ if (publicToken.expires < new Date()) {
throw new DubApiError({
code: "unauthorized",
message: "Public token expired.",
});
}
- link = await prisma.link.findUniqueOrThrow({
- where: {
- id: referralToken.linkId,
- },
- });
-
- if (!link.trackConversion) {
- throw new DubApiError({
- code: "forbidden",
- message: "Conversion tracking is not enabled for this link.",
- });
- }
-
- const workspace = await prisma.project.findUniqueOrThrow({
- where: {
- id: link.projectId!,
- },
- });
-
const { success, limit, reset, remaining } = await ratelimit(
rateLimit,
"1 m",
- ).limit(publicToken);
+ ).limit(tokenFromHeader);
headers = {
"Retry-After": reset.toString(),
@@ -119,6 +100,25 @@ export const withAuth = (handler: WithAuthHandler) => {
});
}
+ link = await prisma.link.findUniqueOrThrow({
+ where: {
+ id: publicToken.linkId,
+ },
+ });
+
+ if (!link.trackConversion) {
+ throw new DubApiError({
+ code: "forbidden",
+ message: "Conversion tracking is not enabled for this link.",
+ });
+ }
+
+ const workspace = await prisma.project.findUniqueOrThrow({
+ where: {
+ id: link.projectId!,
+ },
+ });
+
return await handler({
req,
params,