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.

allow users to create new client secrets

Kiran Kf7e45d99fa1a
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/manage/[integrationId]/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/manage/[integrationId]/page-client.tsx
index e2d2a18684..0b1fd84c48 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/manage/[integrationId]/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/manage/[integrationId]/page-client.tsx
@@ -1,5 +1,6 @@
"use client";
+import { generateClientSecret } from "@/lib/actions/generate-client-secret";
import useWorkspace from "@/lib/swr/use-workspace";
import { OAuthAppProps } from "@/lib/types";
import { useRemoveIntegrationModal } from "@/ui/modals/remove-integration-modal";
@@ -13,10 +14,12 @@ import {
TokenAvatar,
} from "@dub/ui";
import { cn, fetcher } from "@dub/utils";
-import { ChevronLeft, Trash } from "lucide-react";
+import { ChevronLeft, RefreshCcw, Trash } from "lucide-react";
+import { useAction } from "next-safe-action/hooks";
import Link from "next/link";
import { notFound, useSearchParams } from "next/navigation";
import { useState } from "react";
+import { toast } from "sonner";
import useSWR from "swr";
import AddEditIntegrationForm from "../../../../../../../ui/integrations/add-edit-integration-form";
import OAuthAppCredentials from "../../../../../../../ui/integrations/oauth-app-credentials";
@@ -29,6 +32,7 @@ export default function IntegrationManagePageClient({
const searchParams = useSearchParams();
const { slug, id: workspaceId } = useWorkspace();
const [openPopover, setOpenPopover] = useState(false);
+ const { executeAsync, result, status } = useAction(generateClientSecret);
const { data: integration, isLoading } = useSWR<OAuthAppProps>(
`/api/oauth/apps/${integrationId}?workspaceId=${workspaceId}`,
fetcher,
@@ -83,6 +87,29 @@ export default function IntegrationManagePageClient({
align="end"
content={
<div className="grid w-screen gap-px p-2 sm:w-48">
+ <Button
+ text={
+ status === "executing" ? "Generating..." : "Generate secret"
+ }
+ variant="outline"
+ icon={<RefreshCcw className="h-4 w-4" />}
+ className="h-9 justify-start px-2 font-medium"
+ disabled={status === "executing"}
+ onClick={async () => {
+ await executeAsync({
+ workspaceId: workspaceId!,
+ integrationId,
+ });
+
+ if (status === "hasSucceeded") {
+ toast.success("New client secret generated.");
+ } else if (status === "hasErrored") {
+ toast.error(result.serverError?.serverError);
+ }
+
+ setOpenPopover(false);
+ }}
+ />
<Button
text="Remove Integration"
variant="danger-outline"
@@ -114,7 +141,11 @@ export default function IntegrationManagePageClient({
<MaxWidthWrapper className="max-w-screen-lg space-y-10">
<OAuthAppCredentials
clientId={integration.clientId}
- clientSecret={searchParams.get("client_secret") || null}
+ clientSecret={
+ searchParams.get("client_secret") ||
+ result.data?.clientSecret ||
+ null
+ }
/>
<AddEditIntegrationForm integration={integration} />
</MaxWidthWrapper>
diff --git a/apps/web/lib/actions/generate-client-secret.ts b/apps/web/lib/actions/generate-client-secret.ts
new file mode 100644
index 0000000000..07392b2a60
--- /dev/null
+++ b/apps/web/lib/actions/generate-client-secret.ts
@@ -0,0 +1,44 @@
+"use server";
+
+import { prisma } from "@/lib/prisma";
+import { OAUTH_CONFIG } from "../api/oauth/constants";
+import { createToken } from "../api/oauth/utils";
+import { hashToken } from "../auth";
+import z from "../zod";
+import { authActionClient } from "./safe-action";
+
+const schema = z.object({
+ workspaceId: z.string(),
+ integrationId: z.string(),
+});
+
+// Generate a new client secret for an integration
+export const generateClientSecret = authActionClient
+ .schema(schema)
+ .action(async ({ ctx, parsedInput }) => {
+ const { workspace } = ctx;
+ const { integrationId } = parsedInput;
+
+ await prisma.oAuthApp.findFirstOrThrow({
+ where: {
+ id: integrationId,
+ projectId: workspace.id,
+ },
+ });
+
+ const clientSecret = createToken({
+ length: OAUTH_CONFIG.CLIENT_SECRET_LENGTH,
+ prefix: OAUTH_CONFIG.CLIENT_SECRET_PREFIX,
+ });
+
+ await prisma.oAuthApp.update({
+ where: {
+ id: integrationId,
+ },
+ data: {
+ hashedClientSecret: await hashToken(clientSecret),
+ },
+ });
+
+ return { clientSecret };
+ });
diff --git a/apps/web/lib/actions/update-notification-preference.ts b/apps/web/lib/actions/update-notification-preference.ts
index f5dbeefe2e..ff351fa46c 100644
--- a/apps/web/lib/actions/update-notification-preference.ts
+++ b/apps/web/lib/actions/update-notification-preference.ts
@@ -10,6 +10,7 @@ const schema = z.object({
value: z.boolean(),
});
+// Update the notification preference for a user in a workspace
export const updateNotificationPreference = authActionClient
.schema(schema)
.action(async ({ ctx, parsedInput }) => {