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.

improve edge config efficiency

Steven Tey27fd2d367484
diff --git a/apps/web/app/api/workspaces/route.ts b/apps/web/app/api/workspaces/route.ts
index 86cb0cebbc..a5d5760948 100644
--- a/apps/web/app/api/workspaces/route.ts
+++ b/apps/web/app/api/workspaces/route.ts
@@ -5,6 +5,7 @@ import {
} from "@/lib/api/domains";
import { DubApiError } from "@/lib/api/errors";
import { withSession } from "@/lib/auth";
+import { checkIfUserExists } from "@/lib/planetscale";
import prisma from "@/lib/prisma";
import { createWorkspaceSchema } from "@/lib/zod/schemas";
import { FREE_WORKSPACES_LIMIT, nanoid } from "@dub/utils";
@@ -47,6 +48,15 @@ export const POST = withSession(async ({ req, session }) => {
await req.json(),
);
+ const userExists = await checkIfUserExists(session.user.id);
+
+ if (!userExists) {
+ throw new DubApiError({
+ code: "not_found",
+ message: "Session expired. Please log in again.",
+ });
+ }
+
const freeWorkspaces = await prisma.project.count({
where: {
plan: "free",
diff --git a/apps/web/lib/api/links/process-link.ts b/apps/web/lib/api/links/process-link.ts
index ddc26dda2d..a8851f24c0 100644
--- a/apps/web/lib/api/links/process-link.ts
+++ b/apps/web/lib/api/links/process-link.ts
@@ -1,6 +1,6 @@
import { isBlacklistedDomain, updateConfig } from "@/lib/edge-config";
import { getPangeaDomainIntel } from "@/lib/pangea";
-import { getRandomKey } from "@/lib/planetscale";
+import { checkIfUserExists, getRandomKey } from "@/lib/planetscale";
import prisma from "@/lib/prisma";
import { LinkWithTagIdsProps, WorkspaceProps } from "@/lib/types";
import {
@@ -99,6 +99,18 @@ export async function processLink({
// checks for dub.sh links
if (domain === "dub.sh") {
+ // check if user exists (if userId is passed)
+ if (userId) {
+ const userExists = await checkIfUserExists(userId);
+ if (!userExists) {
+ return {
+ link: payload,
+ error: "Session expired. Please log in again.",
+ code: "not_found",
+ };
+ }
+ }
+
const isMaliciousLink = await maliciousLinkCheck(url);
if (isMaliciousLink) {
return {
diff --git a/apps/web/lib/auth/options.ts b/apps/web/lib/auth/options.ts
index 330fe5b013..2792ff1580 100644
--- a/apps/web/lib/auth/options.ts
+++ b/apps/web/lib/auth/options.ts
@@ -276,11 +276,6 @@ export const authOptions: NextAuthOptions = {
return true;
},
jwt: async ({ token, user, trigger }) => {
- // force log out banned users
- if (!token.email || (await isBlacklistedEmail(token.email))) {
- return {};
- }
-
if (user) {
token.user = user;
}
diff --git a/apps/web/lib/planetscale.ts b/apps/web/lib/planetscale.ts
index 3602b86e45..5c1104bf71 100644
--- a/apps/web/lib/planetscale.ts
+++ b/apps/web/lib/planetscale.ts
@@ -47,6 +47,16 @@ export const checkIfKeyExists = async (domain: string, key: string) => {
return rows && Array.isArray(rows) && rows.length > 0;
};
+export const checkIfUserExists = async (userId: string) => {
+ if (!DATABASE_URL) return null;
+
+ const { rows } =
+ (await conn.execute("SELECT 1 FROM User WHERE id = ? LIMIT 1", [userId])) ||
+ {};
+
+ return rows && Array.isArray(rows) && rows.length > 0;
+};
+
export const getLinkViaEdge = async (domain: string, key: string) => {
if (!DATABASE_URL) return null;