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.

create secret if not passed

Kiran Kb823c6bf3e9a
diff --git a/apps/web/app/api/webhooks/route.ts b/apps/web/app/api/webhooks/route.ts
index 7cb5cba431..5ce36bf5c3 100644
--- a/apps/web/app/api/webhooks/route.ts
+++ b/apps/web/app/api/webhooks/route.ts
@@ -5,6 +5,7 @@ import { withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import { webhookCache } from "@/lib/webhook/cache";
import { WEBHOOK_ID_PREFIX } from "@/lib/webhook/constants";
+import { createWebhookSecret } from "@/lib/webhook/secret";
import { transformWebhook } from "@/lib/webhook/transform";
import { isLinkLevelWebhook } from "@/lib/webhook/utils";
import { createWebhookSchema } from "@/lib/zod/schemas/webhooks";
@@ -53,9 +54,14 @@ export const GET = withWorkspace(
// POST /api/webhooks/ - create a new webhook
export const POST = withWorkspace(
async ({ req, workspace, session }) => {
- const { name, url, secret, triggers, linkIds } = createWebhookSchema.parse(
- await parseRequestBody(req),
- );
+ const body = createWebhookSchema.parse(await parseRequestBody(req));
+
+ let { secret } = body;
+ const { name, url, triggers, linkIds } = body;
+
+ if (!secret) {
+ secret = createWebhookSecret();
+ }
const existingWebhook = await prisma.webhook.findFirst({
where: {
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/webhooks/new/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/webhooks/new/page.tsx
index 4c19a1862b..036256a5b8 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/webhooks/new/page.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/webhooks/new/page.tsx
@@ -1,15 +1,6 @@
-import { createToken } from "@/lib/api/oauth/utils";
-import {
- WEBHOOK_SECRET_LENGTH,
- WEBHOOK_SECRET_PREFIX,
-} from "@/lib/webhook/constants";
+import { createWebhookSecret } from "@/lib/webhook/secret";
import NewWebhookPageClient from "./page-client";
export default async function NewWebhookPage() {
- const newSecret = createToken({
- prefix: WEBHOOK_SECRET_PREFIX,
- length: WEBHOOK_SECRET_LENGTH,
- });
-
- return <NewWebhookPageClient newSecret={newSecret} />;
+ return <NewWebhookPageClient newSecret={createWebhookSecret()} />;
}
diff --git a/apps/web/lib/webhook/secret.ts b/apps/web/lib/webhook/secret.ts
new file mode 100644
index 0000000000..e478c6fb8e
--- /dev/null
+++ b/apps/web/lib/webhook/secret.ts
@@ -0,0 +1,12 @@
+import {
+ WEBHOOK_SECRET_LENGTH,
+ WEBHOOK_SECRET_PREFIX,
+} from "@/lib/webhook/constants";
+import { createToken } from "../api/oauth/utils";
+
+export const createWebhookSecret = () => {
+ return createToken({
+ prefix: WEBHOOK_SECRET_PREFIX,
+ length: WEBHOOK_SECRET_LENGTH,
+ });
+};
diff --git a/apps/web/lib/zod/schemas/webhooks.ts b/apps/web/lib/zod/schemas/webhooks.ts
index bb37f86d77..a2dace7543 100644
--- a/apps/web/lib/zod/schemas/webhooks.ts
+++ b/apps/web/lib/zod/schemas/webhooks.ts
@@ -1,7 +1,4 @@
-import {
- WEBHOOK_SECRET_PREFIX,
- WEBHOOK_TRIGGERS,
-} from "@/lib/webhook/constants";
+import { WEBHOOK_TRIGGERS } from "@/lib/webhook/constants";
import { z } from "zod";
export const webhookSchema = z.object({
@@ -17,7 +14,7 @@ export const webhookSchema = z.object({
export const createWebhookSchema = z.object({
name: z.string().min(1).max(30),
url: z.string().url().max(190),
- secret: z.string().startsWith(WEBHOOK_SECRET_PREFIX),
+ secret: z.string().optional(),
triggers: z.array(z.enum(WEBHOOK_TRIGGERS)),
linkIds: z.array(z.string()).optional(),
});