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.

make workspaceId optional for restricted token

Kiran K688fdacbf149
diff --git a/apps/web/lib/auth/workspace-edge.ts b/apps/web/lib/auth/workspace-edge.ts
index 9fc8b464fa..3ea7387c08 100644
--- a/apps/web/lib/auth/workspace-edge.ts
+++ b/apps/web/lib/auth/workspace-edge.ts
@@ -90,15 +90,17 @@ export const withWorkspaceEdge = (
let workspaceSlug: string | undefined;
let scopes: Scope[] = [];
let token: any | null = null;
+ const isRestrictedToken = apiKey?.startsWith("dub_");
- const idOrSlug =
+ let idOrSlug =
params?.idOrSlug ||
searchParams.workspaceId ||
params?.slug ||
searchParams.projectSlug;
- // if there's no workspace ID or slug
- if (!idOrSlug) {
+ // if there's no workspace ID or slug and it's not a restricted token
+ // For restricted tokens, we find the workspaceId from the token
+ if (!idOrSlug && !isRestrictedToken) {
throw new DubApiError({
code: "not_found",
message:
@@ -107,7 +109,6 @@ export const withWorkspaceEdge = (
}
if (apiKey) {
- const isRestrictedToken = apiKey.startsWith("dub_");
const hashedKey = await hashToken(apiKey);
const prismaArgs = {
where: {
@@ -164,6 +165,11 @@ export const withWorkspaceEdge = (
});
}
+ // Find workspaceId if it's a restricted token
+ if (isRestrictedToken) {
+ idOrSlug = `ws_${token.projectId}`;
+ }
+
waitUntil(
// update last used time for the token
(async () => {
@@ -235,7 +241,7 @@ export const withWorkspaceEdge = (
},
})) as WorkspaceProps;
- if (!workspace || !workspace.users) {
+ if (!workspace || !workspace.users.length) {
// workspace doesn't exist
throw new DubApiError({
code: "not_found",
diff --git a/apps/web/lib/auth/workspace.ts b/apps/web/lib/auth/workspace.ts
index 532f456f20..9831c18681 100644
--- a/apps/web/lib/auth/workspace.ts
+++ b/apps/web/lib/auth/workspace.ts
@@ -111,15 +111,17 @@ export const withWorkspace = (
let workspaceSlug: string | undefined;
let scopes: Scope[] = [];
let token: any | null = null;
+ const isRestrictedToken = apiKey?.startsWith("dub_");
- const idOrSlug =
+ let idOrSlug =
params?.idOrSlug ||
searchParams.workspaceId ||
params?.slug ||
searchParams.projectSlug;
- // if there's no workspace ID or slug
- if (!idOrSlug) {
+ // if there's no workspace ID or slug and it's not a restricted token
+ // For restricted tokens, we find the workspaceId from the token
+ if (!idOrSlug && !isRestrictedToken) {
// for /api/links (POST /api/links) – allow no session (but warn if user provides apiKey)
if (allowAnonymous && !apiKey) {
// @ts-expect-error
@@ -139,7 +141,6 @@ export const withWorkspace = (
}
if (apiKey) {
- const isRestrictedToken = apiKey.startsWith("dub_");
const hashedKey = await hashToken(apiKey);
const prismaArgs = {
where: {
@@ -196,6 +197,11 @@ export const withWorkspace = (
});
}
+ // Find workspaceId if it's a restricted token
+ if (isRestrictedToken) {
+ idOrSlug = `ws_${token.projectId}`;
+ }
+
waitUntil(
// update last used time for the token
(async () => {
@@ -296,7 +302,7 @@ export const withWorkspace = (
])) as [WorkspaceProps, LinkProps | undefined];
// workspace doesn't exist
- if (!workspace || !workspace.users) {
+ if (!workspace || !workspace.users.length) {
throw new DubApiError({
code: "not_found",
message: "Workspace not found.",