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.

list the authorized apps to the workspace

Kiran K1d99b5b591c8
diff --git a/apps/web/app/api/oauth-apps/authorized/route.ts b/apps/web/app/api/oauth-apps/authorized/route.ts
new file mode 100644
index 0000000000..8ae541f3ce
--- /dev/null
+++ b/apps/web/app/api/oauth-apps/authorized/route.ts
@@ -0,0 +1,33 @@
+import { withWorkspace } from "@/lib/auth";
+import { prisma } from "@/lib/prisma";
+import { NextResponse } from "next/server";
+
+// GET /api/oauth-apps/authorized - applications authorized to access workspace resources
+export const GET = withWorkspace(
+ async ({ workspace }) => {
+ const appsAuthorized = await prisma.oAuthClient.findMany({
+ where: {
+ accessTokens: {
+ some: {
+ projectId: workspace.id,
+ },
+ },
+ },
+ select: {
+ clientId: true,
+ name: true,
+ developer: true,
+ website: true,
+ scopes: true,
+ createdAt: true,
+ },
+ });
+
+ console.log(appsAuthorized);
+
+ return NextResponse.json(appsAuthorized);
+ },
+ {
+ requiredScopes: ["workspaces.read"],
+ },
+);
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/page-client.tsx
new file mode 100644
index 0000000000..99292226d8
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/page-client.tsx
@@ -0,0 +1,111 @@
+"use client";
+
+import useWorkspace from "@/lib/swr/use-workspace";
+import { OAuthAuthorizedAppProps } from "@/lib/types";
+import EmptyState from "@/ui/shared/empty-state";
+import { Button, LoadingSpinner, Popover, TokenAvatar } from "@dub/ui";
+import { Key } from "@dub/ui/src/icons";
+import { fetcher } from "@dub/utils";
+import { Edit3, MoreVertical } from "lucide-react";
+import { useState } from "react";
+import useSWR from "swr";
+
+export default function AuthorizedAppsPageClient() {
+ const { id: workspaceId } = useWorkspace();
+ const { data: apps, isLoading } = useSWR<OAuthAuthorizedAppProps[]>(
+ `/api/oauth-apps/authorized?workspaceId=${workspaceId}`,
+ fetcher,
+ );
+
+ return (
+ <>
+ <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">Integrations Enabled</h2>
+ <p className="text-sm text-gray-500">
+ Applications that have access to your workspace resources.
+ </p>
+ </div>
+ </div>
+ {isLoading || !apps ? (
+ <div className="flex flex-col items-center justify-center space-y-4 py-20">
+ <LoadingSpinner className="h-6 w-6 text-gray-500" />
+ <p className="text-sm text-gray-500">
+ Fetching authorized applications...
+ </p>
+ </div>
+ ) : apps.length > 0 ? (
+ <div>
+ <div className="grid grid-cols-5 border-b border-gray-200 px-5 py-2 text-sm font-medium text-gray-500 sm:px-10">
+ <div className="col-span-3">Name</div>
+ </div>
+ <div className="divide-y divide-gray-200">
+ {apps.map((token) => (
+ <AppRow key={token.clientId} {...token} />
+ ))}
+ </div>
+ </div>
+ ) : (
+ <div className="flex flex-col items-center justify-center gap-y-4 py-20">
+ <EmptyState
+ icon={Key}
+ title="You have not yet authorized any applications to connect with your Linear account."
+ />
+ </div>
+ )}
+ </div>
+ </>
+ );
+}
+
+const AppRow = (app: OAuthAuthorizedAppProps) => {
+ const [openPopover, setOpenPopover] = useState(false);
+
+ return (
+ <>
+ <div className="relative grid grid-cols-5 items-center px-5 py-3 sm:px-10">
+ <div className="col-span-3 flex items-center space-x-3">
+ <TokenAvatar id={app.clientId} />
+ <div className="flex flex-col space-y-px">
+ <p className="font-semibold text-gray-700">{app.name}</p>
+ <div className="flex items-center gap-x-2">
+ <p className="text-sm text-gray-500" suppressHydrationWarning>
+ {app.scopes.length} access scopes
+ </p>
+ </div>
+ </div>
+ </div>
+ <Popover
+ content={
+ <div className="w-full sm:w-48">
+ <div className="grid gap-px p-2">
+ <Button
+ text="Disconnect"
+ variant="danger-outline"
+ icon={<Edit3 className="h-4 w-4" />}
+ className="h-9 justify-start px-2 font-medium"
+ onClick={() => {
+ setOpenPopover(false);
+ }}
+ />
+ </div>
+ </div>
+ }
+ align="end"
+ openPopover={openPopover}
+ setOpenPopover={setOpenPopover}
+ >
+ <button
+ onClick={() => {
+ setOpenPopover(!openPopover);
+ }}
+ className="absolute right-4 rounded-md px-1 py-2 transition-all duration-75 hover:bg-gray-100 active:bg-gray-200"
+ >
+ <MoreVertical className="h-5 w-5 text-gray-500" />
+ </button>
+ </Popover>
+ </div>
+ </>
+ );
+};
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/page.tsx
new file mode 100644
index 0000000000..a0eb11f982
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/page.tsx
@@ -0,0 +1,5 @@
+import AuthorizedAppsPageClient from "./page-client";
+
+export default function AuthorizedAppsPage() {
+ return <AuthorizedAppsPageClient />;
+}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/layout.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/layout.tsx
index 1c9c43a296..a782df371f 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/layout.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/layout.tsx
@@ -23,6 +23,10 @@ export default function WorkspaceSettingsLayout({
name: "API Keys",
segment: "tokens",
},
+ {
+ name: "Integrations",
+ segment: "integrations",
+ },
{
name: "OAuth Apps",
segment: "oauth-apps",
diff --git a/apps/web/lib/api/oauth.ts b/apps/web/lib/api/oauth.ts
index d3cd8733bd..2813e5a02f 100644
--- a/apps/web/lib/api/oauth.ts
+++ b/apps/web/lib/api/oauth.ts
@@ -1,9 +1,9 @@
export const TOKEN_LENGTH = {
clientId: 32,
clientSecret: 32,
- accessToken: 32,
+ accessToken: 40,
refreshToken: 40,
- code: 64,
+ code: 40,
};
export const TOKEN_EXPIRY = {
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index b3e71239bf..a79a96ef56 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -3,7 +3,7 @@ import { metaTagsSchema } from "@/lib/zod/schemas/metatags";
import { DirectorySyncProviders } from "@boxyhq/saml-jackson";
import { Link } from "@prisma/client";
import { createLinkBodySchema } from "./zod/schemas/links";
-import { oAuthClientSchema } from "./zod/schemas/oauth";
+import { authorizedAppSchema, oAuthClientSchema } from "./zod/schemas/oauth";
import { tokenSchema } from "./zod/schemas/token";
export type LinkProps = Link;
@@ -197,3 +197,5 @@ export type MetaTag = z.infer<typeof metaTagsSchema>;
export type TokenProps = z.infer<typeof tokenSchema>;
export type OAuthAppProps = z.infer<typeof oAuthClientSchema>;
+
+export type OAuthAuthorizedAppProps = z.infer<typeof authorizedAppSchema>;
diff --git a/apps/web/lib/zod/schemas/oauth.ts b/apps/web/lib/zod/schemas/oauth.ts
index 1c1d6b183b..b07f946806 100644
--- a/apps/web/lib/zod/schemas/oauth.ts
+++ b/apps/web/lib/zod/schemas/oauth.ts
@@ -90,3 +90,12 @@ export const tokenGrantSchema = z.discriminatedUnion(
}),
},
);
+
+export const authorizedAppSchema = oAuthClientSchema.pick({
+ clientId: true,
+ name: true,
+ developer: true,
+ website: true,
+ scopes: true,
+ createdAt: true,
+});