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 withAuth function and use workspaceId as idOrSlug in project-id component

Kiran Kdb8fc01aba46
diff --git a/apps/web/lib/auth/index.ts b/apps/web/lib/auth/index.ts
index 0c71686f63..8a4ac89f26 100644
--- a/apps/web/lib/auth/index.ts
+++ b/apps/web/lib/auth/index.ts
@@ -117,13 +117,18 @@ export const withAuth = (
let session: Session | undefined;
let headers = {};
+ let id: string | undefined;
+ let slug: string | undefined;
- const slug = params?.slug || searchParams.projectSlug;
- const workspaceId = params?.workspaceId || searchParams.workspaceId;
+ const idOrSlug =
+ params?.slug ||
+ searchParams.projectSlug ||
+ params?.workspaceId ||
+ searchParams.workspaceId;
try {
- // if there's no projectSlug defined
- if (!slug && !workspaceId) {
+ // if there's no projectSlug & id defined
+ if (!idOrSlug) {
// for /api/links (POST /api/links) – allow no session (but warn if user provides apiKey)
if (allowAnonymous && !apiKey) {
// @ts-expect-error
@@ -142,6 +147,12 @@ export const withAuth = (
}
}
+ if (idOrSlug.startsWith("w_")) {
+ id = idOrSlug.replace("w_", "");
+ } else {
+ slug = idOrSlug;
+ }
+
if (apiKey) {
const hashedKey = hashToken(apiKey, {
noSecret: true,
@@ -215,7 +226,7 @@ export const withAuth = (
prisma.project.findUnique({
where: {
slug: slug || undefined,
- id: workspaceId || undefined,
+ id: id || undefined,
},
select: {
id: true,
diff --git a/apps/web/ui/projects/project-id.tsx b/apps/web/ui/projects/project-id.tsx
index 10b510d8e8..19293e1f95 100644
--- a/apps/web/ui/projects/project-id.tsx
+++ b/apps/web/ui/projects/project-id.tsx
@@ -5,6 +5,7 @@ import { CopyButton } from "@dub/ui";
export default function ProjectID() {
const { id } = useProject();
+ const workspaceId = `w_${id}`
return (
<>
@@ -17,8 +18,8 @@ export default function ProjectID() {
</p>
</div>
<div className="flex w-full max-w-md items-center justify-between rounded-md border border-gray-300 bg-white p-2">
- <p className="text-sm text-gray-500">{id}</p>
- <CopyButton value={id!} className="rounded-md" />
+ <p className="text-sm text-gray-500">{workspaceId}</p>
+ <CopyButton value={workspaceId} className="rounded-md" />
</div>
</div>
<div className="flex items-center justify-between rounded-b-lg border-t border-gray-200 bg-gray-50 px-3 py-5 sm:px-10">