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 workspace to handle restricted tokens

Kiran Ka1792f01e096
diff --git a/apps/web/lib/auth/workspace-edge.ts b/apps/web/lib/auth/workspace-edge.ts
index 3ea7387c08..e4c5289fe9 100644
--- a/apps/web/lib/auth/workspace-edge.ts
+++ b/apps/web/lib/auth/workspace-edge.ts
@@ -92,7 +92,7 @@ export const withWorkspaceEdge = (
let token: any | null = null;
const isRestrictedToken = apiKey?.startsWith("dub_");
- let idOrSlug =
+ const idOrSlug =
params?.idOrSlug ||
searchParams.workspaceId ||
params?.slug ||
@@ -108,6 +108,12 @@ export const withWorkspaceEdge = (
});
}
+ if (idOrSlug.startsWith("ws_")) {
+ workspaceId = idOrSlug.replace("ws_", "");
+ } else {
+ workspaceSlug = idOrSlug;
+ }
+
if (apiKey) {
const hashedKey = await hashToken(apiKey);
const prismaArgs = {
@@ -167,7 +173,7 @@ export const withWorkspaceEdge = (
// Find workspaceId if it's a restricted token
if (isRestrictedToken) {
- idOrSlug = `ws_${token.projectId}`;
+ workspaceId = token.projectId;
}
waitUntil(
@@ -212,12 +218,6 @@ export const withWorkspaceEdge = (
}
}
- if (idOrSlug.startsWith("ws_")) {
- workspaceId = idOrSlug.replace("ws_", "");
- } else {
- workspaceSlug = idOrSlug;
- }
-
const workspace = (await prismaEdge.project.findUnique({
where: {
id: workspaceId || undefined,
diff --git a/apps/web/lib/auth/workspace.ts b/apps/web/lib/auth/workspace.ts
index 9831c18681..8ccbfd3e2d 100644
--- a/apps/web/lib/auth/workspace.ts
+++ b/apps/web/lib/auth/workspace.ts
@@ -113,7 +113,7 @@ export const withWorkspace = (
let token: any | null = null;
const isRestrictedToken = apiKey?.startsWith("dub_");
- let idOrSlug =
+ const idOrSlug =
params?.idOrSlug ||
searchParams.workspaceId ||
params?.slug ||
@@ -140,6 +140,12 @@ export const withWorkspace = (
}
}
+ if (idOrSlug.startsWith("ws_")) {
+ workspaceId = idOrSlug.replace("ws_", "");
+ } else {
+ workspaceSlug = idOrSlug;
+ }
+
if (apiKey) {
const hashedKey = await hashToken(apiKey);
const prismaArgs = {
@@ -199,7 +205,7 @@ export const withWorkspace = (
// Find workspaceId if it's a restricted token
if (isRestrictedToken) {
- idOrSlug = `ws_${token.projectId}`;
+ workspaceId = token.projectId;
}
waitUntil(
@@ -241,12 +247,6 @@ export const withWorkspace = (
}
}
- if (idOrSlug.startsWith("ws_")) {
- workspaceId = idOrSlug.replace("ws_", "");
- } else {
- workspaceSlug = idOrSlug;
- }
-
let [workspace, link] = (await Promise.all([
prisma.project.findUnique({
where: {
@@ -316,6 +316,8 @@ export const withWorkspace = (
scopes = roleScopesMapping[workspace.users[0].role];
}
+ console.log(workspace);
+
// Check user has permission to make the action
if (!skipScopeChecks) {
throwIfNoAccess({