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.

Add `isDubAdmin` check

Steven Teya87ceb75356f
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
index 05fd9a8a3d..29ac6cf762 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
@@ -1,4 +1,5 @@
-import { withWorkspace } from "@/lib/auth";
+import { DubApiError } from "@/lib/api/errors";
+import { isDubAdmin, withWorkspace } from "@/lib/auth";
import { getDubCustomer } from "@/lib/dub";
import { stripe } from "@/lib/stripe";
import { APP_DOMAIN } from "@dub/utils";
@@ -26,6 +27,16 @@ export const POST = withWorkspace(async ({ req, workspace, session }) => {
.then((res) => res.data[0])
: null;
+ if (process.env.VERCEL === "1" && process.env.VERCEL_ENV === "preview") {
+ const isAdminUser = await isDubAdmin(session.user.id);
+ if (!isAdminUser) {
+ throw new DubApiError({
+ code: "unauthorized",
+ message: "Unauthorized: Not an admin.",
+ });
+ }
+ }
+
// if the user has an active subscription, create billing portal to upgrade
if (workspace.stripeId && activeSubscription) {
const { url } = await stripe.billingPortal.sessions.create({
diff --git a/apps/web/lib/auth/admin.ts b/apps/web/lib/auth/admin.ts
index 58f927e1f2..d50b096eb1 100644
--- a/apps/web/lib/auth/admin.ts
+++ b/apps/web/lib/auth/admin.ts
@@ -15,6 +15,21 @@ interface WithAdminHandler {
}): Promise<Response>;
}
+export const isDubAdmin = async (userId: string) => {
+ const response = await prisma.projectUsers.findUnique({
+ where: {
+ userId_projectId: {
+ userId,
+ projectId: DUB_WORKSPACE_ID,
+ },
+ },
+ });
+ if (!response) {
+ return false;
+ }
+ return true;
+};
+
export const withAdmin =
(handler: WithAdminHandler) =>
async (
@@ -26,15 +41,8 @@ export const withAdmin =
return new Response("Unauthorized: Login required.", { status: 401 });
}
- const response = await prisma.projectUsers.findUnique({
- where: {
- userId_projectId: {
- userId: session.user.id,
- projectId: DUB_WORKSPACE_ID,
- },
- },
- });
- if (!response) {
+ const isAdminUser = await isDubAdmin(session.user.id);
+ if (!isAdminUser) {
return new Response("Unauthorized: Not an admin.", { status: 401 });
}