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.

Move the oauth app creation form from a modal to a new page under /integrations/new

Kiran Kddd71528380e
diff --git a/apps/web/app/api/oauth/apps/[clientId]/route.ts b/apps/web/app/api/oauth/apps/[appId]/route.ts
similarity index 70%
rename from apps/web/app/api/oauth/apps/[clientId]/route.ts
rename to apps/web/app/api/oauth/apps/[appId]/route.ts
index 35d1e99706..0e157cbff1 100644
--- a/apps/web/app/api/oauth/apps/[clientId]/route.ts
+++ b/apps/web/app/api/oauth/apps/[appId]/route.ts
@@ -5,12 +5,12 @@ import { prisma } from "@/lib/prisma";
import { oAuthAppSchema, updateOAuthAppSchema } from "@/lib/zod/schemas/oauth";
import { NextResponse } from "next/server";
-// GET /api/oauth/apps/[clientId] – get an OAuth app created by the workspace
+// GET /api/oauth/apps/[appId] – get an OAuth app created by the workspace
export const GET = withWorkspace(
async ({ params, workspace }) => {
const app = await prisma.oAuthApp.findFirst({
where: {
- clientId: params.clientId,
+ id: params.appId,
projectId: workspace.id,
},
});
@@ -18,7 +18,7 @@ export const GET = withWorkspace(
if (!app) {
throw new DubApiError({
code: "not_found",
- message: `OAuth app with id ${params.clientId} not found.`,
+ message: `OAuth app with id ${params.appId} not found.`,
});
}
@@ -30,15 +30,23 @@ export const GET = withWorkspace(
},
);
-// PATCH /api/oauth/apps/[clientId] – update an OAuth app
+// PATCH /api/oauth/apps/[appId] – update an OAuth app
export const PATCH = withWorkspace(
async ({ req, params, workspace }) => {
- const { name, slug, developer, website, redirectUri, logo, pkce } =
- updateOAuthAppSchema.parse(await parseRequestBody(req));
+ const {
+ name,
+ slug,
+ developer,
+ website,
+ description,
+ redirectUri,
+ logo,
+ pkce,
+ } = updateOAuthAppSchema.parse(await parseRequestBody(req));
const app = await prisma.oAuthApp.update({
where: {
- clientId: params.clientId,
+ id: params.appId,
projectId: workspace.id,
},
data: {
@@ -46,6 +54,7 @@ export const PATCH = withWorkspace(
slug,
developer,
website,
+ description,
redirectUri,
logo,
pkce,
@@ -60,12 +69,12 @@ export const PATCH = withWorkspace(
},
);
-// DELETE /api/oauth/apps/[clientId] - delete an OAuth app
+// DELETE /api/oauth/apps/[appId] - delete an OAuth app
export const DELETE = withWorkspace(
async ({ params, workspace }) => {
const app = await prisma.oAuthApp.findFirst({
where: {
- clientId: params.clientId,
+ id: params.appId,
projectId: workspace.id,
},
});
@@ -73,17 +82,17 @@ export const DELETE = withWorkspace(
if (!app) {
throw new DubApiError({
code: "not_found",
- message: `OAuth app with id ${params.clientId} not found.`,
+ message: `OAuth app with id ${params.appId} not found.`,
});
}
await prisma.oAuthApp.delete({
where: {
- clientId: params.clientId,
+ id: params.appId,
},
});
- return NextResponse.json({ id: params.clientId });
+ return NextResponse.json({ id: params.appId });
},
{
requiredPermissions: ["oauth_apps.write"],
diff --git a/apps/web/app/api/oauth/apps/route.ts b/apps/web/app/api/oauth/apps/route.ts
index 56feb8901c..c47b758b95 100644
--- a/apps/web/app/api/oauth/apps/route.ts
+++ b/apps/web/app/api/oauth/apps/route.ts
@@ -10,12 +10,22 @@ import { z } from "zod";
// GET /api/oauth/apps - get all OAuth apps created by a workspace
export const GET = withWorkspace(
async ({ workspace }) => {
- const apps = await prisma.oAuthApp.findMany({
+ const result = await prisma.oAuthApp.findMany({
where: {
projectId: workspace.id,
},
+ include: {
+ _count: {
+ select: { authorizedApps: true },
+ },
+ },
});
+ const apps = result.map((app) => ({
+ ...app,
+ installations: app._count.authorizedApps,
+ }));
+
return NextResponse.json(z.array(oAuthAppSchema).parse(apps));
},
{
@@ -27,8 +37,16 @@ export const GET = withWorkspace(
// POST /api/oauth/apps - create a new OAuth app
export const POST = withWorkspace(
async ({ req, workspace, session }) => {
- const { name, slug, developer, website, redirectUri, logo, pkce } =
- createOAuthAppSchema.parse(await parseRequestBody(req));
+ const {
+ name,
+ slug,
+ developer,
+ website,
+ description,
+ redirectUri,
+ logo,
+ pkce,
+ } = createOAuthAppSchema.parse(await parseRequestBody(req));
const clientId = createToken({
length: OAUTH_CONFIG.CLIENT_ID_LENGTH,
@@ -47,6 +65,7 @@ export const POST = withWorkspace(
slug,
developer,
website,
+ description,
redirectUri,
logo,
clientId,
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/add-edit-integration-form.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/add-edit-integration-form.tsx
new file mode 100644
index 0000000000..2fcd9091bf
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/add-edit-integration-form.tsx
@@ -0,0 +1,269 @@
+"use client";
+
+import useWorkspace from "@/lib/swr/use-workspace";
+import {
+ ExistingIntegration,
+ NewIntegration,
+ OAuthAppProps,
+} from "@/lib/types";
+import { Button, InfoTooltip, Switch } from "@dub/ui";
+import slugify from "@sindresorhus/slugify";
+import { redirect, useRouter } from "next/navigation";
+import { FormEvent, useEffect, useMemo, useState } from "react";
+import TextareaAutosize from "react-textarea-autosize";
+import { toast } from "sonner";
+import { mutate } from "swr";
+
+const defaultValues: NewIntegration = {
+ name: "Demo",
+ slug: "demo",
+ description: "",
+ developer: "Kiran",
+ website: "http://localhost:3000/api/oauth/callback",
+ redirectUri: "http://localhost:3000/api/oauth/callback",
+ logo: "http://localhost:3000/api/oauth/callback",
+ pkce: false,
+};
+
+export default function AddEditIntegrationForm({
+ integration,
+}: {
+ integration: OAuthAppProps | null;
+}) {
+ const router = useRouter();
+ const { slug: workspaceSlug, id: workspaceId, flags } = useWorkspace();
+
+ if (!flags?.integrations) {
+ redirect(`/${workspaceSlug}`);
+ }
+
+ const [saving, setSaving] = useState(false);
+ const [data, setData] = useState<NewIntegration | ExistingIntegration>(
+ integration || defaultValues,
+ );
+
+ useEffect(() => {
+ setData((prev) => ({
+ ...prev,
+ slug: slugify(name),
+ }));
+ }, [data.name]);
+
+ // Determine the endpoint
+ const endpoint = useMemo(() => {
+ if (integration) {
+ return {
+ method: "PATCH",
+ url: `/api/oauth/apps/${integration.id}?workspaceId=${workspaceId}`,
+ successMessage: "Integration updated!",
+ };
+ } else {
+ return {
+ method: "POST",
+ url: `/api/oauth/apps?workspaceId=${workspaceId}`,
+ successMessage: "Integration created!",
+ };
+ }
+ }, [integration]);
+
+ // Save the form data
+ const onSubmit = async (e: FormEvent) => {
+ e.preventDefault();
+ setSaving(true);
+
+ const response = await fetch(endpoint.url, {
+ method: endpoint.method,
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify(data),
+ });
+
+ setSaving(false);
+ const result = await response.json();
+
+ if (response.ok) {
+ mutate(`/api/oauth/apps/${result.id}?workspaceId=${workspaceId}`, result);
+ toast.success(endpoint.successMessage);
+ } else {
+ toast.error(result.error.message);
+ }
+
+ if (endpoint.method === "POST") {
+ router.push(
+ `/${workspaceSlug}/integrations/manage/${result.id}?client_secret=${result.clientSecret}`,
+ );
+ }
+ };
+
+ const {
+ name,
+ slug,
+ description,
+ developer,
+ website,
+ redirectUri,
+ logo,
+ pkce,
+ } = data;
+
+ const buttonDisabled =
+ !name || !slug || !developer || !website || !redirectUri;
+
+ return (
+ <>
+ <form
+ onSubmit={onSubmit}
+ className="flex flex-col space-y-4 pb-20 text-left"
+ >
+ <div>
+ <label htmlFor="name" className="flex items-center space-x-2">
+ <h2 className="text-sm font-medium text-gray-900">
+ Application name
+ </h2>
+ <InfoTooltip content="Application name will be displayed in the OAuth consent screen" />
+ </label>
+ <div className="relative mt-2 rounded-md shadow-sm">
+ <input
+ className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
+ required
+ value={name}
+ onChange={(e) => setData({ ...data, name: e.target.value })}
+ autoFocus
+ autoComplete="off"
+ placeholder="My App"
+ />
+ </div>
+ </div>
+
+ <div>
+ <label htmlFor="slug" className="flex items-center space-x-2">
+ <h2 className="text-sm font-medium text-gray-900">
+ Application slug
+ </h2>
+ <InfoTooltip content="Unique slug for this application on Dub" />
+ </label>
+ <div className="relative mt-2 rounded-md shadow-sm">
+ <input
+ className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
+ required
+ value={slug}
+ onChange={(e) => setData({ ...data, slug: e.target.value })}
+ autoComplete="off"
+ placeholder="my-app"
+ />
+ </div>
+ </div>
+
+ <div>
+ <label htmlFor="slug" className="flex items-center space-x-2">
+ <h2 className="text-sm font-medium text-gray-900">Description</h2>
+ <InfoTooltip content="Description of your application" />
+ </label>
+ <div className="relative mt-2 rounded-md shadow-sm">
+ <TextareaAutosize
+ name="comments"
+ minRows={3}
+ className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
+ placeholder="Add a description"
+ value={description || ""}
+ onChange={(e) => {
+ setData({ ...data, description: e.target.value });
+ }}
+ />
+ </div>
+ </div>
+
+ <div>
+ <label htmlFor="developer" className="flex items-center space-x-2">
+ <h2 className="text-sm font-medium text-gray-900">
+ Developer name
+ </h2>
+ <InfoTooltip content="The person or company developing this application" />
+ </label>
+ <div className="relative mt-2 rounded-md shadow-sm">
+ <input
+ className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
+ required
+ value={developer}
+ onChange={(e) => setData({ ...data, developer: e.target.value })}
+ placeholder="Acme Inc."
+ />
+ </div>
+ </div>
+
+ <div>
+ <label htmlFor="website" className="flex items-center space-x-2">
+ <h2 className="text-sm font-medium text-gray-900">Website URL</h2>
+ <InfoTooltip content="URL to the developer's website or documentation" />
+ </label>
+ <div className="relative mt-2 rounded-md shadow-sm">
+ <input
+ className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
+ type="url"
+ required
+ value={website}
+ onChange={(e) => setData({ ...data, website: e.target.value })}
+ placeholder="https://acme.com"
+ />
+ </div>
+ </div>
+
+ <div>
+ <label htmlFor="logo" className="flex items-center space-x-2">
+ <h2 className="text-sm font-medium text-gray-900">Logo URL</h2>
+ <InfoTooltip content="URL to your application's logo" />
+ </label>
+ <div className="relative mt-2 rounded-md shadow-sm">
+ <input
+ className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
+ type="url"
+ value={logo || ""}
+ onChange={(e) => setData({ ...data, logo: e.target.value })}
+ placeholder="https://acme.com/logo.png"
+ />
+ </div>
+ </div>
+
+ <div>
+ <label htmlFor="redirectUri" className="flex items-center space-x-2">
+ <h2 className="text-sm font-medium text-gray-900">Callback URL</h2>
+ <InfoTooltip content="URL to redirect the user after authentication. Must use HTTPS, except for localhost" />
+ </label>
+ <div className="relative mt-2 rounded-md shadow-sm">
+ <input
+ className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
+ type="url"
+ required
+ value={redirectUri}
+ onChange={(e) =>
+ setData({ ...data, redirectUri: e.target.value })
+ }
+ placeholder="https://acme.com/callback"
+ />
+ </div>
+ </div>
+
+ <div className="flex items-center justify-between pb-4 pt-2">
+ <label htmlFor="pkce" className="flex items-center space-x-2">
+ <h2 className="text-sm font-medium text-gray-900">Allow PKCE</h2>
+ <InfoTooltip content="We strongly recommend using the PKCE flow for increased security" />
+ </label>
+ <Switch
+ checked={pkce}
+ fn={(value: boolean) => {
+ setData({ ...data, pkce: value });
+ }}
+ />
+ </div>
+
+ <Button
+ text={integration ? "Save changes" : "Create"}
+ disabled={buttonDisabled}
+ loading={saving}
+ type="submit"
+ />
+ </form>
+ </>
+ );
+}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/installed/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/installed/page-client.tsx
deleted file mode 100644
index 59b66b2166..0000000000
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/installed/page-client.tsx
+++ /dev/null
@@ -1,3 +0,0 @@
-export default function InstalledIntegrationsPage() {
- return <div>Installed Integrations</div>;
-}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/installed/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/installed/page.tsx
deleted file mode 100644
index 00063d23aa..0000000000
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/installed/page.tsx
+++ /dev/null
@@ -1,3 +0,0 @@
-export default function InstalledIntegrations() {
- return <div>InstalledIntegrations</div>;
-}
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
new file mode 100644
index 0000000000..cb9a93488f
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/manage/[integrationId]/page-client.tsx
@@ -0,0 +1,174 @@
+"use client";
+
+import useWorkspace from "@/lib/swr/use-workspace";
+import { OAuthAppProps } from "@/lib/types";
+import { ThreeDots } from "@/ui/shared/icons";
+import {
+ BlurImage,
+ Button,
+ CopyButton,
+ LoadingSpinner,
+ MaxWidthWrapper,
+ Popover,
+ TokenAvatar,
+} from "@dub/ui";
+import { cn, fetcher } from "@dub/utils";
+import { ChevronLeft, Trash } from "lucide-react";
+import Link from "next/link";
+import { notFound, useSearchParams } from "next/navigation";
+import { useState } from "react";
+import useSWR from "swr";
+import AddEditIntegrationForm from "../../add-edit-integration-form";
+
+export default function IntegrationManagePageClient({
+ integrationId,
+}: {
+ integrationId: string;
+}) {
+ const searchParams = useSearchParams();
+ const { slug, id: workspaceId } = useWorkspace();
+ const [openPopover, setOpenPopover] = useState(false);
+ const { data: integration, isLoading } = useSWR<OAuthAppProps>(
+ `/api/oauth/apps/${integrationId}?workspaceId=${workspaceId}`,
+ fetcher,
+ );
+
+ // const { UninstallIntegrationModal, setShowUninstallIntegrationModal } =
+ // useUninstallIntegrationModal({
+ // integration: integration,
+ // });
+
+ if (isLoading) {
+ return <LoadingSpinner />;
+ }
+
+ if (!integration) {
+ return notFound();
+ }
+
+ return (
+ <>
+ <MaxWidthWrapper className="my-10 grid max-w-screen-lg gap-8">
+ {/* <UninstallIntegrationModal /> */}
+ <Link
+ href={`/${slug}/integrations/manage`}
+ className="flex items-center gap-x-1"
+ >
+ <ChevronLeft className="size-4" />
+ <p className="text-sm font-medium text-gray-500">Integrations</p>
+ </Link>
+ <div className="flex justify-between">
+ <div className="flex items-center gap-x-3">
+ <div className="rounded-md border border-gray-200 bg-gradient-to-t from-gray-100 p-2">
+ {integration.logo ? (
+ <BlurImage
+ src={integration.logo}
+ alt={`Logo for ${integration.name}`}
+ className="size-8 rounded-full border border-gray-200"
+ width={20}
+ height={20}
+ />
+ ) : (
+ <TokenAvatar id={integration.clientId} className="size-8" />
+ )}
+ </div>
+ <div>
+ <p className="font-semibold text-gray-700">{integration.name}</p>
+ <p className="text-sm text-gray-500">{integration.description}</p>
+ </div>
+ </div>
+
+ <Popover
+ align="end"
+ content={
+ <div className="grid w-screen gap-px p-2 sm:w-48">
+ <Button
+ text="Remove Integration"
+ variant="danger-outline"
+ icon={<Trash className="h-4 w-4" />}
+ className="h-9 justify-start px-2"
+ onClick={() => {
+ // setShowUninstallIntegrationModal(true);
+ }}
+ />
+ </div>
+ }
+ openPopover={openPopover}
+ setOpenPopover={setOpenPopover}
+ >
+ <button
+ onClick={() => setOpenPopover(!openPopover)}
+ className={cn(
+ "flex h-10 items-center rounded-md border px-1.5 outline-none transition-all",
+ "border-gray-200 bg-white text-gray-900 placeholder-gray-400",
+ "focus-visible:border-gray-500 data-[state=open]:border-gray-500 data-[state=open]:ring-4 data-[state=open]:ring-gray-200",
+ )}
+ >
+ <ThreeDots className="h-5 w-5 text-gray-500" />
+ </button>
+ </Popover>
+ </div>
+
+ {/* <div className="flex gap-12 rounded-lg border border-gray-200 bg-white p-4">
+ <div className="flex items-center gap-2">
+ <AddEditIntegrationForm />
+ </div>
+ </div> */}
+ </MaxWidthWrapper>
+
+ <MaxWidthWrapper className="max-w-screen-lg space-y-10">
+ <AppCredentials
+ clientId={integration.clientId}
+ clientSecret={searchParams.get("client_secret") || null}
+ />
+ <AddEditIntegrationForm integration={integration} />
+ </MaxWidthWrapper>
+ </>
+ );
+}
+
+const AppCredentials = ({
+ clientId,
+ clientSecret,
+}: {
+ clientId: string | null;
+ clientSecret: string | null;
+}) => {
+ if (!clientId) {
+ return null;
+ }
+
+ return (
+ <div className="flex flex-col space-y-4 bg-gray-50 text-left">
+ <div className="space-y-2">
+ <label className="text-sm font-medium text-gray-500">Client ID</label>
+ <div className="flex items-center justify-between rounded-md border border-gray-300 bg-white p-3">
+ <p className="font-mono text-sm text-gray-500">{clientId}</p>
+ <div className="flex flex-col gap-2">
+ <CopyButton value={clientId} className="rounded-md" />
+ </div>
+ </div>
+ </div>
+
+ {clientSecret && (
+ <div className="space-y-2">
+ <label className="text-sm font-medium text-gray-500">
+ Client Secret
+ </label>
+ <div className="flex items-center justify-between rounded-md border border-gray-300 bg-white p-3">
+ <p className="text-nowrap font-mono text-sm text-gray-500">
+ {clientSecret}
+ </p>
+ <div className="flex flex-col gap-2">
+ <CopyButton value={clientSecret} className="rounded-md" />
+ </div>
+ </div>
+ <span className="text-xs text-red-400">
+ Be sure to copy your client secret. You won’t be able to see it
+ again.
+ </span>
+ </div>
+ )}
+ </div>
+ );
+};
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/manage/[integrationId]/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/manage/[integrationId]/page.tsx
new file mode 100644
index 0000000000..8a9a6a7d1f
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/manage/[integrationId]/page.tsx
@@ -0,0 +1,11 @@
+import IntegrationManagePageClient from "./page-client";
+
+export default async function IntegrationManagePage({
+ params,
+}: {
+ params: { integrationId: string };
+}) {
+ const { integrationId } = params;
+
+ return <IntegrationManagePageClient integrationId={integrationId} />;
+}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/manage/integration-card.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/manage/integration-card.tsx
new file mode 100644
index 0000000000..b2716e97ef
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/manage/integration-card.tsx
@@ -0,0 +1,48 @@
+import useWorkspace from "@/lib/swr/use-workspace";
+import { OAuthAppProps } from "@/lib/types";
+import { BlurImage, Download, TokenAvatar } from "@dub/ui";
+import { OfficeBuilding } from "@dub/ui/src/icons";
+import Link from "next/link";
+
+export default function IntegrationCard(integration: OAuthAppProps) {
+ const { slug } = useWorkspace();
+
+ return (
+ <Link
+ href={`/${slug}/integrations/manage/${integration.id}`}
+ className="relative rounded-xl border border-gray-200 bg-white px-5 py-4 transition-[filter] hover:[filter:drop-shadow(0_8px_12px_#222A350d)_drop-shadow(0_32px_80px_#2f30370f)]"
+ >
+ <div className="flex items-center gap-x-3">
+ <div className="rounded-md border border-gray-200 bg-gradient-to-t from-gray-100 p-2.5">
+ {integration.logo ? (
+ <BlurImage
+ src={integration.logo}
+ alt={`Logo for ${integration.name}`}
+ className="size-6 rounded-full border border-gray-200"
+ width={20}
+ height={20}
+ />
+ ) : (
+ <TokenAvatar id={integration.clientId} className="size-6" />
+ )}
+ </div>
+ <div>
+ <p className="font-semibold text-gray-700">{integration.name}</p>
+ <div className="flex items-center gap-1 text-gray-500">
+ <OfficeBuilding className="size-3" />
+ <span className="text-sm">{integration.developer}</span>
+ </div>
+ </div>
+ </div>
+ <div className="items-between grid h-24 pt-4">
+ <p className="line-clamp-3 text-sm text-gray-500">
+ {integration.description}
+ </p>
+ <div className="flex items-center justify-end gap-1 text-gray-500">
+ <Download className="size-4" />
+ <span className="text-sm">{integration.installations} installs</span>
+ </div>
+ </div>
+ </Link>
+ );
+}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/manage/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/manage/page-client.tsx
new file mode 100644
index 0000000000..4f5129290b
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/manage/page-client.tsx
@@ -0,0 +1,56 @@
+"use client";
+
+import useWorkspace from "@/lib/swr/use-workspace";
+import { OAuthAppProps } from "@/lib/types";
+import { MaxWidthWrapper, buttonVariants } from "@dub/ui";
+import { cn, fetcher } from "@dub/utils";
+import Link from "next/link";
+import { redirect } from "next/navigation";
+import useSWR from "swr";
+import IntegrationCard from "./integration-card";
+
+export default function IntegrationConsolePageClient() {
+ const { slug, id: workspaceId, flags } = useWorkspace();
+
+ if (!flags?.integrations) {
+ redirect(`/${slug}`);
+ }
+
+ const { data: integrations, isLoading } = useSWR<OAuthAppProps[]>(
+ `/api/oauth/apps?workspaceId=${workspaceId}`,
+ fetcher,
+ );
+
+ return (
+ <>
+ <div className="flex h-36 w-full items-center border-b border-gray-200 bg-white">
+ <MaxWidthWrapper>
+ <div className="flex items-center justify-between">
+ <h1 className="text-2xl font-semibold tracking-tight text-black">
+ Integrations
+ </h1>
+ <div className="flex gap-2">
+ <Link
+ href={`/${slug}/integrations/new`}
+ className={cn(
+ buttonVariants({ variant: "primary" }),
+ "flex h-10 items-center justify-center whitespace-nowrap rounded-lg border px-4 text-sm",
+ )}
+ >
+ Create Integration
+ </Link>
+ </div>
+ </div>
+ </MaxWidthWrapper>
+ </div>
+
+ <MaxWidthWrapper className="flex flex-col gap-3 py-4">
+ <div className="grid gap-4 sm:grid-cols-3">
+ {integrations?.map((integration) => (
+ <IntegrationCard key={integration.id} {...integration} />
+ ))}
+ </div>
+ </MaxWidthWrapper>
+ </>
+ );
+}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/manage/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/manage/page.tsx
new file mode 100644
index 0000000000..73a9ffafa4
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/manage/page.tsx
@@ -0,0 +1,5 @@
+import IntegrationConsolePageClient from "./page-client";
+
+export default async function IntegrationConsolePage() {
+ return <IntegrationConsolePageClient />;
+}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/new/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/new/page-client.tsx
new file mode 100644
index 0000000000..c0135eb36f
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/new/page-client.tsx
@@ -0,0 +1,45 @@
+"use client";
+
+import useWorkspace from "@/lib/swr/use-workspace";
+import { buttonVariants, MaxWidthWrapper } from "@dub/ui";
+import { cn } from "@dub/utils";
+import Link from "next/link";
+import { redirect } from "next/navigation";
+import AddEditIntegrationForm from "../add-edit-integration-form";
+
+export default function NewIntegrationPageClient() {
+ const { slug: workspaceSlug, flags } = useWorkspace();
+
+ if (!flags?.integrations) {
+ redirect(`/${workspaceSlug}`);
+ }
+
+ return (
+ <>
+ <div className="flex h-36 w-full items-center border-b border-gray-200 bg-white">
+ <MaxWidthWrapper>
+ <div className="flex items-center justify-between">
+ <h1 className="text-2xl font-semibold tracking-tight text-black">
+ Integrations
+ </h1>
+ <div className="flex gap-2">
+ <Link
+ href={`/${workspaceSlug}/integrations/manage`}
+ className={cn(
+ buttonVariants({ variant: "secondary" }),
+ "flex h-10 items-center justify-center whitespace-nowrap rounded-lg border px-4 text-sm",
+ )}
+ >
+ My Integrations
+ </Link>
+ </div>
+ </div>
+ </MaxWidthWrapper>
+ </div>
+
+ <MaxWidthWrapper className="my-10 grid max-w-screen-lg gap-8">
+ <AddEditIntegrationForm integration={null} />
+ </MaxWidthWrapper>
+ </>
+ );
+}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/new/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/new/page.tsx
new file mode 100644
index 0000000000..9405d0e4bf
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/new/page.tsx
@@ -0,0 +1,5 @@
+import NewIntegrationPageClient from "./page-client";
+
+export default async function IntegrationsPage() {
+ return <NewIntegrationPageClient />;
+}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/page-client.tsx
index 695c24acf0..030a233991 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/page-client.tsx
@@ -2,13 +2,10 @@
import useWorkspace from "@/lib/swr/use-workspace";
import { OAuthAppProps } from "@/lib/types";
-import { useAddEditAppModal } from "@/ui/modals/add-edit-oauth-app-modal";
-import { useAppCreatedModal } from "@/ui/modals/oauth-app-created-modal";
-import { Button, MaxWidthWrapper, buttonVariants } from "@dub/ui";
+import { MaxWidthWrapper, buttonVariants } from "@dub/ui";
import { cn } from "@dub/utils";
import Link from "next/link";
import { redirect } from "next/navigation";
-import { useState } from "react";
import IntegrationCard from "./integration-card";
export default function IntegrationsPageClient({
@@ -25,26 +22,8 @@ export default function IntegrationsPageClient({
redirect(`/${slug}`);
}
- const [app, setApp] = useState<OAuthAppProps | null>(null);
- const { AppCreatedModal, setShowAppCreatedModal } = useAppCreatedModal({
- app,
- });
-
- const onAppCreated = (app: OAuthAppProps) => {
- if (!app) return;
-
- setApp(app);
- setShowAppCreatedModal(true);
- };
-
- const { AddEditAppModal, setShowAddEditAppModal } = useAddEditAppModal({
- onAppCreated,
- });
-
return (
<>
- <AddEditAppModal />
- <AppCreatedModal />
<div className="flex h-36 w-full items-center border-b border-gray-200 bg-white">
<MaxWidthWrapper>
<div className="flex items-center justify-between">
@@ -52,12 +31,17 @@ export default function IntegrationsPageClient({
Integrations
</h1>
<div className="flex gap-2">
- <Button
- text="Create Integration"
- onClick={() => setShowAddEditAppModal(true)}
- />
<Link
- href={`/${slug}/integrations/console`}
+ href={`/${slug}/integrations/new`}
+ className={cn(
+ buttonVariants({ variant: "primary" }),
+ "flex h-10 items-center justify-center whitespace-nowrap rounded-lg border px-4 text-sm",
+ )}
+ >
+ Create Integration
+ </Link>
+ <Link
+ href={`/${slug}/integrations/manage`}
className={cn(
buttonVariants({ variant: "secondary" }),
"flex h-10 items-center justify-center whitespace-nowrap rounded-lg border px-4 text-sm",
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 4b190d2109..0e19c1b1ac 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -221,3 +221,10 @@ export type InstalledIntegrationProps = z.infer<
> & {
createdAt: string;
};
+
+export type NewIntegration = Omit<
+ OAuthAppProps,
+ "id" | "clientId" | "verified" | "installations"
+>;
+
+export type ExistingIntegration = OAuthAppProps;
diff --git a/apps/web/lib/zod/schemas/oauth.ts b/apps/web/lib/zod/schemas/oauth.ts
index e17c340294..7c88d8925b 100644
--- a/apps/web/lib/zod/schemas/oauth.ts
+++ b/apps/web/lib/zod/schemas/oauth.ts
@@ -35,12 +35,14 @@ export const createOAuthAppSchema = z.object({
"redirect_uri must be a valid URL starting with 'https://' except for 'http://localhost'",
},
),
+ description: z.string().max(5000).nullable(),
pkce: z.boolean().default(false),
});
export const updateOAuthAppSchema = createOAuthAppSchema.partial();
export const oAuthAppSchema = z.object({
+ id: z.string(),
clientId: z.string(),
clientSecret: z.string().optional(),
name: z.string(),
@@ -52,6 +54,7 @@ export const oAuthAppSchema = z.object({
logo: z.string().nullable(),
pkce: z.boolean(),
verified: z.boolean(),
+ installations: z.number().default(0),
});
// Schema for OAuth2.0 Authorization request
diff --git a/apps/web/ui/modals/add-edit-oauth-app-modal.tsx b/apps/web/ui/modals/add-edit-oauth-app-modal.tsx
deleted file mode 100644
index 60ab35ae97..0000000000
--- a/apps/web/ui/modals/add-edit-oauth-app-modal.tsx
+++ /dev/null
@@ -1,337 +0,0 @@
-import useWorkspace from "@/lib/swr/use-workspace";
-import { OAuthAppProps } from "@/lib/types";
-import { Button, ButtonProps, InfoTooltip, Logo, Modal, Switch } from "@dub/ui";
-import slugify from "@sindresorhus/slugify";
-import {
- Dispatch,
- FormEvent,
- SetStateAction,
- useCallback,
- useEffect,
- useMemo,
- useState,
-} from "react";
-import TextareaAutosize from "react-textarea-autosize";
-import { toast } from "sonner";
-import { mutate } from "swr";
-
-const newApp: OAuthAppProps = {
- name: "",
- slug: "",
- description: "",
- developer: "",
- website: "",
- clientId: "",
- redirectUri: "",
- logo: null,
- pkce: false,
- verified: false,
-};
-
-function AddEditAppModal({
- showAddEditAppModal,
- setShowAddEditAppModal,
- app,
- onAppCreated,
-}: {
- showAddEditAppModal: boolean;
- setShowAddEditAppModal: Dispatch<SetStateAction<boolean>>;
- app?: OAuthAppProps;
- onAppCreated?: (App: OAuthAppProps) => void;
-}) {
- const [saving, setSaving] = useState(false);
- const { id: workspaceId } = useWorkspace();
- const [data, setData] = useState<OAuthAppProps>(app || newApp);
-
- // Determine the endpoint
- const endpoint = useMemo(() => {
- if (app) {
- return {
- method: "PATCH",
- url: `/api/integrations/${app.clientId}?workspaceId=${workspaceId}`,
- successMessage: "OAuth app updated!",
- };
- } else {
- return {
- method: "POST",
- url: `/api/integrations?workspaceId=${workspaceId}`,
- successMessage: "OAuth app created!",
- };
- }
- }, [app]);
-
- // Save the form data
- const onSubmit = async (e: FormEvent) => {
- e.preventDefault();
- setSaving(true);
-
- const response = await fetch(endpoint.url, {
- method: endpoint.method,
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify(data),
- });
-
- const result = await response.json();
-
- if (response.ok) {
- mutate(`/api/integrations?workspaceId=${workspaceId}`);
- toast.success(endpoint.successMessage);
- onAppCreated?.(result);
- setShowAddEditAppModal(false);
- } else {
- toast.error(result.error.message);
- setSaving(false);
- }
- };
-
- const {
- name,
- slug,
- description,
- developer,
- website,
- redirectUri,
- logo,
- pkce,
- } = data;
-
- useEffect(() => {
- setData((prev) => ({
- ...prev,
- slug: slugify(name),
- }));
- }, [name]);
-
- const buttonDisabled =
- !name || !slug || !developer || !website || !redirectUri;
-
- return (
- <>
- <Modal
- showModal={showAddEditAppModal}
- setShowModal={setShowAddEditAppModal}
- className="scrollbar-hide h-fit max-h-[95vh] overflow-auto"
- >
- <div className="flex flex-col items-center justify-center gap-1 border-b border-gray-200 px-4 py-4 pt-8 text-center sm:px-16">
- <Logo className="mb-3" />
- <h1 className="text-lg font-medium">
- {app ? "Update" : "Create"} OAuth Application
- </h1>
- <p className="text-sm text-gray-500">
- Allow users to sign in to your application using their Dub account.
- </p>
- </div>
-
- <form
- onSubmit={onSubmit}
- className="flex flex-col space-y-4 bg-gray-50 px-4 py-8 text-left sm:px-10"
- >
- <div>
- <label htmlFor="name" className="flex items-center space-x-2">
- <h2 className="text-sm font-medium text-gray-900">
- Application name
- </h2>
- <InfoTooltip content="Application name will be displayed in the OAuth consent screen" />
- </label>
- <div className="relative mt-2 rounded-md shadow-sm">
- <input
- className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
- required
- value={name}
- onChange={(e) => setData({ ...data, name: e.target.value })}
- autoFocus
- autoComplete="off"
- placeholder="My App"
- />
- </div>
- </div>
-
- <div>
- <label htmlFor="slug" className="flex items-center space-x-2">
- <h2 className="text-sm font-medium text-gray-900">
- Application slug
- </h2>
- <InfoTooltip content="Unique slug for this application on Dub" />
- </label>
- <div className="relative mt-2 rounded-md shadow-sm">
- <input
- className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
- required
- value={slug}
- onChange={(e) => setData({ ...data, slug: e.target.value })}
- autoComplete="off"
- placeholder="my-app"
- />
- </div>
- </div>
-
- <div>
- <label htmlFor="slug" className="flex items-center space-x-2">
- <h2 className="text-sm font-medium text-gray-900">Description</h2>
- <InfoTooltip content="Description of your application" />
- </label>
- <div className="relative mt-2 rounded-md shadow-sm">
- <TextareaAutosize
- name="comments"
- minRows={3}
- className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
- placeholder="Add a description"
- value={description || ""}
- onChange={(e) => {
- setData({ ...data, description: e.target.value });
- }}
- />
- </div>
- </div>
-
- <div>
- <label htmlFor="developer" className="flex items-center space-x-2">
- <h2 className="text-sm font-medium text-gray-900">
- Developer name
- </h2>
- <InfoTooltip content="The person or company developing this application" />
- </label>
- <div className="relative mt-2 rounded-md shadow-sm">
- <input
- className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
- required
- value={developer}
- onChange={(e) =>
- setData({ ...data, developer: e.target.value })
- }
- placeholder="Acme Inc."
- />
- </div>
- </div>
-
- <div>
- <label htmlFor="website" className="flex items-center space-x-2">
- <h2 className="text-sm font-medium text-gray-900">Website URL</h2>
- <InfoTooltip content="URL to the developer's website or documentation" />
- </label>
- <div className="relative mt-2 rounded-md shadow-sm">
- <input
- className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
- type="url"
- required
- value={website}
- onChange={(e) => setData({ ...data, website: e.target.value })}
- placeholder="https://acme.com"
- />
- </div>
- </div>
-
- <div>
- <label htmlFor="logo" className="flex items-center space-x-2">
- <h2 className="text-sm font-medium text-gray-900">Logo URL</h2>
- <InfoTooltip content="URL to your application's logo" />
- </label>
- <div className="relative mt-2 rounded-md shadow-sm">
- <input
- className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
- type="url"
- value={logo || ""}
- onChange={(e) => setData({ ...data, logo: e.target.value })}
- placeholder="https://acme.com/logo.png"
- />
- </div>
- </div>
-
- <div>
- <label
- htmlFor="redirectUri"
- className="flex items-center space-x-2"
- >
- <h2 className="text-sm font-medium text-gray-900">
- Callback URL
- </h2>
- <InfoTooltip content="URL to redirect the user after authentication. Must use HTTPS, except for localhost" />
- </label>
- <div className="relative mt-2 rounded-md shadow-sm">
- <input
- className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
- type="url"
- required
- value={redirectUri}
- onChange={(e) =>
- setData({ ...data, redirectUri: e.target.value })
- }
- placeholder="https://acme.com/callback"
- />
- </div>
- </div>
-
- <div className="flex items-center justify-between pb-4 pt-2">
- <label htmlFor="pkce" className="flex items-center space-x-2">
- <h2 className="text-sm font-medium text-gray-900">Allow PKCE</h2>
- <InfoTooltip content="We strongly recommend using the PKCE flow for increased security" />
- </label>
- <Switch
- checked={pkce}
- fn={(value: boolean) => {
- setData({ ...data, pkce: value });
- }}
- />
- </div>
-
- <Button
- text={app ? "Save changes" : "Create"}
- disabled={buttonDisabled}
- loading={saving}
- />
- </form>
- </Modal>
- </>
- );
-}
-
-export function AddAppButton({
- setShowAddEditAppModal,
- buttonProps,
-}: {
- setShowAddEditAppModal: Dispatch<SetStateAction<boolean>>;
- buttonProps?: Partial<ButtonProps>;
-}) {
- return (
- <div>
- <Button
- text="Create"
- onClick={() => setShowAddEditAppModal(true)}
- {...buttonProps}
- />
- </div>
- );
-}
-
-export function useAddEditAppModal(
- {
- app,
- onAppCreated,
- }: {
- app?: OAuthAppProps;
- onAppCreated?: (App: OAuthAppProps) => void;
- } = { onAppCreated: () => {} },
-) {
- const [showAddEditAppModal, setShowAddEditAppModal] = useState(false);
-
- const AddEditAppModalCallback = useCallback(() => {
- return (
- <AddEditAppModal
- showAddEditAppModal={showAddEditAppModal}
- setShowAddEditAppModal={setShowAddEditAppModal}
- app={app}
- onAppCreated={onAppCreated}
- />
- );
- }, [showAddEditAppModal, setShowAddEditAppModal]);
-
- return useMemo(
- () => ({
- setShowAddEditAppModal,
- AddEditAppModal: AddEditAppModalCallback,
- }),
- [setShowAddEditAppModal, AddEditAppModalCallback],
- );
-}
diff --git a/apps/web/ui/modals/oauth-app-created-modal.tsx b/apps/web/ui/modals/oauth-app-created-modal.tsx
deleted file mode 100644
index 936757aef0..0000000000
--- a/apps/web/ui/modals/oauth-app-created-modal.tsx
+++ /dev/null
@@ -1,94 +0,0 @@
-import { OAuthAppProps } from "@/lib/types";
-import { Button, CopyButton, Logo, Modal } from "@dub/ui";
-import {
- Dispatch,
- SetStateAction,
- useCallback,
- useMemo,
- useState,
-} from "react";
-
-function AppCreatedModal({
- showAppCreatedModal,
- setShowAppCreatedModal,
- app,
-}: {
- showAppCreatedModal: boolean;
- setShowAppCreatedModal: Dispatch<SetStateAction<boolean>>;
- app: OAuthAppProps | null;
-}) {
- if (!app) {
- return null;
- }
-
- console.log("AppCreatedModal rendered", app);
-
- return (
- <Modal
- showModal={showAppCreatedModal}
- setShowModal={setShowAppCreatedModal}
- >
- <div className="flex flex-col items-center justify-center space-y-3 border-b border-gray-200 px-4 py-4 pt-8 sm:px-16">
- <Logo />
- <h3 className="text-lg font-medium">{app.name}</h3>
- <p className="text-center text-sm text-gray-500">
- Be sure to copy your client secret. You won’t be able to see it again.
- </p>
- </div>
-
- <div className="flex flex-col space-y-4 bg-gray-50 px-4 py-8 text-left sm:px-10">
- <div className="space-y-2">
- <label className="text-sm font-medium text-gray-500">Client ID</label>
- <div className="flex items-center justify-between rounded-md border border-gray-300 bg-white p-3">
- <p className="font-mono text-sm text-gray-500">{app.clientId}</p>
- <div className="flex flex-col gap-2">
- <CopyButton value={app.clientId} className="rounded-md" />
- </div>
- </div>
- </div>
-
- <div className="space-y-2">
- <label className="text-sm font-medium text-gray-500">
- Client Secret
- </label>
- <div className="flex items-center justify-between rounded-md border border-gray-300 bg-white p-3">
- <p className="text-nowrap font-mono text-sm text-gray-500">
- {app.clientSecret}
- </p>
- <div className="flex flex-col gap-2">
- <CopyButton value={app.clientSecret!} className="rounded-md" />
- </div>
- </div>
- </div>
-
- <Button
- text="Done"
- variant="secondary"
- onClick={() => setShowAppCreatedModal(false)}
- />
- </div>
- </Modal>
- );
-}
-
-export function useAppCreatedModal({ app }: { app: OAuthAppProps | null }) {
- const [showAppCreatedModal, setShowAppCreatedModal] = useState(false);
-
- const AppCreatedModalCallback = useCallback(() => {
- return (
- <AppCreatedModal
- showAppCreatedModal={showAppCreatedModal}
- setShowAppCreatedModal={setShowAppCreatedModal}
- app={app}
- />
- );
- }, [showAppCreatedModal, setShowAppCreatedModal]);
-
- return useMemo(
- () => ({
- setShowAppCreatedModal,
- AppCreatedModal: AppCreatedModalCallback,
- }),
- [setShowAppCreatedModal, AppCreatedModalCallback],
- );
-}