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.

Reapply "add publishable key"

Kiran Kf54672cc41fa
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/tokens/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/tokens/page-client.tsx
index fef6aee9e8..9523dc69a4 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/tokens/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/tokens/page-client.tsx
@@ -8,6 +8,7 @@ import { useDeleteTokenModal } from "@/ui/modals/delete-token-modal";
import { useTokenCreatedModal } from "@/ui/modals/token-created-modal";
import EmptyState from "@/ui/shared/empty-state";
import { Delete } from "@/ui/shared/icons";
+import { WorkspacePublicKey } from "@/ui/workspaces/workspace-public-key";
import {
Avatar,
Badge,
@@ -24,7 +25,7 @@ import { useState } from "react";
import useSWR from "swr";
export default function TokensPageClient() {
- const { id: workspaceId } = useWorkspace();
+ const { id: workspaceId, conversionEnabled } = useWorkspace();
const { data: tokens, isLoading } = useSWR<TokenProps[]>(
`/api/tokens?workspaceId=${workspaceId}`,
fetcher,
@@ -48,10 +49,11 @@ export default function TokensPageClient() {
<>
<TokenCreatedModal />
<AddEditTokenModal />
+ {conversionEnabled && <WorkspacePublicKey />}
<div className="rounded-lg border border-gray-200 bg-white">
<div className="flex flex-col items-center justify-between gap-4 space-y-3 border-b border-gray-200 p-5 sm:flex-row sm:space-y-0 sm:p-10">
<div className="flex max-w-screen-sm flex-col space-y-3">
- <h2 className="text-xl font-medium">Workspace API Keys</h2>
+ <h2 className="text-xl font-medium">Secret keys</h2>
<p className="text-sm text-gray-500">
These API keys allow other apps to access your workspace. Use it
with caution – do not share your API key with others, or expose it
diff --git a/apps/web/lib/actions/create-publishable-key.ts b/apps/web/lib/actions/create-publishable-key.ts
new file mode 100644
index 0000000000..b519743d1b
--- /dev/null
+++ b/apps/web/lib/actions/create-publishable-key.ts
@@ -0,0 +1,40 @@
+"use server";
+
+import { prisma } from "@/lib/prisma";
+import { nanoid } from "@dub/utils";
+import z from "../zod";
+import { authActionClient } from "./safe-action";
+
+const schema = z.object({
+ workspaceId: z.string(),
+});
+
+// Create publishable key
+export const createPublishableKey = authActionClient
+ .schema(schema)
+ .action(async ({ ctx }) => {
+ const { workspace } = ctx;
+
+ if (!workspace.conversionEnabled) {
+ throw new Error("Conversion is not enabled for this workspace.");
+ }
+
+ if (workspace.publishableKey) {
+ throw new Error("Publishable key already exists.");
+ }
+
+ const publishableKey = `dub_pk_${nanoid(24)}`;
+
+ await prisma.project.update({
+ where: {
+ id: workspace.id,
+ },
+ data: {
+ publishableKey,
+ },
+ });
+
+ return {
+ publishableKey,
+ };
+ });
diff --git a/apps/web/lib/zod/schemas/workspaces.ts b/apps/web/lib/zod/schemas/workspaces.ts
index d1571973b4..0e093ef13f 100644
--- a/apps/web/lib/zod/schemas/workspaces.ts
+++ b/apps/web/lib/zod/schemas/workspaces.ts
@@ -104,6 +104,10 @@ export const WorkspaceSchema = z
.describe(
"The feature flags of the workspace, indicating which features are enabled.",
),
+ publishableKey: z
+ .string()
+ .nullable()
+ .describe("The publishable key of the workspace."),
})
.openapi({
title: "Workspace",
diff --git a/apps/web/prisma/schema/workspace.prisma b/apps/web/prisma/schema/workspace.prisma
index 3365278031..b7c8734c94 100644
--- a/apps/web/prisma/schema/workspace.prisma
+++ b/apps/web/prisma/schema/workspace.prisma
@@ -34,6 +34,7 @@ model Project {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
usageLastChecked DateTime @default(now())
+ publishableKey String? @unique
users ProjectUsers[]
invites ProjectInvite[]
diff --git a/apps/web/ui/workspaces/workspace-public-key.tsx b/apps/web/ui/workspaces/workspace-public-key.tsx
new file mode 100644
index 0000000000..b6c5257a50
--- /dev/null
+++ b/apps/web/ui/workspaces/workspace-public-key.tsx
@@ -0,0 +1,68 @@
+"use client";
+
+import { createPublishableKey } from "@/lib/actions/create-publishable-key";
+import useWorkspace from "@/lib/swr/use-workspace";
+import { Button, CopyButton } from "@dub/ui";
+import { useAction } from "next-safe-action/hooks";
+import { useState } from "react";
+import { toast } from "sonner";
+import { mutate } from "swr";
+
+export const WorkspacePublicKey = () => {
+ const { id, slug, publishableKey } = useWorkspace();
+
+ const [publicKey, setPublicKey] = useState<string | null>(
+ publishableKey || null,
+ );
+
+ const { executeAsync, isExecuting } = useAction(createPublishableKey, {
+ onSuccess({ data }) {
+ toast.success("Publishable key created successfully.");
+
+ if (data?.publishableKey) {
+ setPublicKey(data.publishableKey);
+ }
+
+ mutate(`/api/workspaces/${slug}`);
+ },
+ onError({ error }) {
+ toast.error(error.serverError?.serverError);
+ },
+ });
+
+ return (
+ <>
+ <div className="rounded-lg border border-gray-200 bg-white">
+ <div className="relative flex flex-col space-y-6 p-5 sm:p-10">
+ <div className="flex flex-col space-y-3">
+ <h2 className="text-xl font-medium">Publishable Key</h2>
+ <p className="text-sm text-gray-500">
+ This key should be used in your client-side code.{" "}
+ <a
+ href="https://dub.co/docs/api-reference/tokens"
+ target="_blank"
+ className="font-medium underline underline-offset-4 hover:text-black"
+ >
+ Learn more.
+ </a>
+ </p>
+ </div>
+
+ {publicKey ? (
+ <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">{publicKey}</p>
+ <CopyButton value={publicKey} className="rounded-md" />
+ </div>
+ ) : (
+ <Button
+ text={isExecuting ? "Creating..." : "Create publishable key"}
+ loading={isExecuting}
+ className="w-fit"
+ onClick={() => executeAsync({ workspaceId: id! })}
+ />
+ )}
+ </div>
+ </div>
+ </>
+ );
+};