The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Update the way we handle anonymous link creation
Steven Tey05de1827b8b1
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.tsindex cec3c585cd..6f405ffea1 100644--- a/apps/web/app/api/links/route.ts+++ b/apps/web/app/api/links/route.ts@@ -126,7 +126,6 @@ export const POST = withWorkspace(}},{- allowAnonymous: true,requiredPermissions: ["links.write"],},);diff --git a/apps/web/lib/auth/workspace.ts b/apps/web/lib/auth/workspace.tsindex 3eb4723f12..a91fb42a92 100644--- a/apps/web/lib/auth/workspace.ts+++ b/apps/web/lib/auth/workspace.ts@@ -53,14 +53,12 @@ export const withWorkspace = ("enterprise",], // if the action needs a specific planrequiredAddOn,- allowAnonymous, // special case for /api/links (POST /api/links) – allow no sessionfeatureFlag, // if the action needs a specific feature flagrequiredPermissions = [],skipPermissionChecks, // if the action doesn't need to check for required permission(s)}: {requiredPlan?: Array<PlanProps>;requiredAddOn?: AddOns;- allowAnonymous?: boolean;featureFlag?: BetaFeatures;requiredPermissions?: PermissionAction[];skipPermissionChecks?: boolean;@@ -105,8 +103,8 @@ export const withWorkspace = (// if there's no workspace ID or slug and it's not a restricted token// For restricted tokens, we find the workspaceId from the tokenif (!idOrSlug && !isRestrictedToken) {- // for /api/links (POST /api/links) – allow no session (but warn if user provides apiKey)- if (allowAnonymous && !apiKey) {+ // special case for anonymous link creation+ if (req.headers.has("dub-anonymous-link-creation")) {// @ts-expect-errorreturn await handler({req,@@ -114,19 +112,19 @@ export const withWorkspace = (searchParams,headers,});+ // missing authorization header+ } else if (!authorizationHeader) {+ throw new DubApiError({+ code: "unauthorized",+ message: "Missing Authorization header.",+ });+ // in case user is still using personal API keys} else {- if (!authorizationHeader) {- throw new DubApiError({- code: "unauthorized",- message: "Missing Authorization header.",- });- } else {- throw new DubApiError({- code: "not_found",- message:- "Workspace ID not found. Did you forget to include a `workspaceId` query parameter? It looks like you might be using personal API keys, we also recommend refactoring to workspace API keys: https://d.to/keys",- });- }+ throw new DubApiError({+ code: "not_found",+ message:+ "Workspace ID not found. Did you forget to include a `workspaceId` query parameter? It looks like you might be using personal API keys, we also recommend refactoring to workspace API keys: https://d.to/keys",+ });}}