The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
wip
Kiran K1d17af4dfce4
diff --git a/apps/web/app/app.dub.co/(auth)/oauth/authorize/AuthorizeForm.tsx b/apps/web/app/app.dub.co/(auth)/oauth/authorize/AuthorizeForm.tsxindex 86e61ab87b..23a0b59dd5 100644--- a/apps/web/app/app.dub.co/(auth)/oauth/authorize/AuthorizeForm.tsx+++ b/apps/web/app/app.dub.co/(auth)/oauth/authorize/AuthorizeForm.tsx@@ -1,25 +1,44 @@"use client";import { handleAuthorize } from "@/lib/oauth";+import useWorkspaces from "@/lib/swr/use-workspaces";import z from "@/lib/zod";import { authorizeSchema } from "@/lib/zod/schemas/oauth";-import { Button } from "@dub/ui";-import { OAuthClient, Project } from "@prisma/client";+import { Button, InputSelect, InputSelectItemProps } from "@dub/ui";+import { DICEBEAR_AVATAR_URL } from "@dub/utils";+import { OAuthClient } from "@prisma/client";+import { useSession } from "next-auth/react";+import { useEffect, useMemo, useState } from "react";-type AuthorizeFormProps = {- workspaces: Pick<Project, "id" | "name" | "logo">[];+interface AuthorizeFormProps extends z.infer<typeof authorizeSchema> {oAuthClient: Pick<OAuthClient, "name">;-} & z.infer<typeof authorizeSchema>;+}export const AuthorizeForm = (props: AuthorizeFormProps) => {- const {- workspaces,- oAuthClient,- client_id,- redirect_uri,- response_type,- state,- } = props;+ const { oAuthClient, client_id, redirect_uri, response_type, state } = props;++ const { workspaces } = useWorkspaces();+ const { data: session } = useSession();+ const [selectedWorkspace, setSelectedWorkspace] =+ useState<InputSelectItemProps | null>(null);++ const selectOptions = useMemo(() => {+ return workspaces+ ? workspaces.map((workspace) => ({+ id: workspace.id,+ value: workspace.name,+ image: workspace.logo || `${DICEBEAR_AVATAR_URL}${workspace.name}`,+ }))+ : [];+ }, [workspaces]);++ useEffect(() => {+ setSelectedWorkspace(+ selectOptions.find(+ (option) => option.id === session?.user?.["defaultWorkspace"],+ ) || selectOptions[0],+ );+ }, [selectOptions, session]);return (<form action={handleAuthorize}>@@ -27,26 +46,31 @@ export const AuthorizeForm = (props: AuthorizeFormProps) => {<input type="hidden" name="redirect_uri" value={redirect_uri} /><input type="hidden" name="response_type" value={response_type} /><input type="hidden" name="state" value={state} />- <select name="workspaceId">- {workspaces.map((workspace) => (- <option key={workspace.id} value={workspace.id}>- <div>- <img- src={workspace.logo!}- className="h-8 w-8 rounded-full"- alt=""- />- {workspace.name}- </div>- </option>- ))}- </select>- <Button- text={`Authorize ${oAuthClient.name}`}- type="submit"- // loading={clickedGithub}- // icon={<Github className="h-4 w-4" />}- />+ <input type="hidden" name="workspaceId" value={selectedWorkspace?.id} />+ <p className="text-sm text-gray-500">+ Select a workspace to grant API access to+ </p>+ <div className="max-w-md py-2">+ <InputSelect+ items={selectOptions}+ selectedItem={selectedWorkspace}+ setSelectedItem={setSelectedWorkspace}+ adjustForMobile+ />+ </div>+ <div className="mt-4 flex justify-between gap-4">+ <Button+ text="Decline"+ type="button"+ onClick={() => {}}+ variant="secondary"+ />+ <Button+ text={`Authorize ${oAuthClient.name}`}+ type="submit"+ // loading={clickedGithub}+ />+ </div></form>);};diff --git a/apps/web/app/app.dub.co/(auth)/oauth/authorize/Scopes.tsx b/apps/web/app/app.dub.co/(auth)/oauth/authorize/Scopes.tsxnew file mode 100644index 0000000000..d150a647da--- /dev/null+++ b/apps/web/app/app.dub.co/(auth)/oauth/authorize/Scopes.tsx@@ -0,0 +1,32 @@+"use client";++import { Check } from "lucide-react";++interface ScopesProps {+ scopes: string | null // space separated scopes+}++export const Scopes = (props: ScopesProps) => {+ const { scopes } = props;+ console.log(scopes);++ return (+ <>+ <span className="text-gray-600">Grant permissions:</span>+ <ul className="text-md space-y-2">+ <li className="flex items-center gap-2">+ <Check className="h-4 w-4 text-green-500" />+ <span className="font-bold">Read</span> access to workspace data+ </li>+ <li className="flex items-center gap-2">+ <Check className="h-4 w-4 text-green-500" />+ <span className="font-bold">Read</span> access to workspace data+ </li>+ <li className="flex items-center gap-2">+ <Check className="h-4 w-4 text-green-500" />+ <span className="font-bold">Read</span> access to workspace data+ </li>+ </ul>+ </>+ );+};diff --git a/apps/web/app/app.dub.co/(auth)/oauth/authorize/page.tsx b/apps/web/app/app.dub.co/(auth)/oauth/authorize/page.tsxindex 0ecb8c67a8..a2c5a25061 100644--- a/apps/web/app/app.dub.co/(auth)/oauth/authorize/page.tsx+++ b/apps/web/app/app.dub.co/(auth)/oauth/authorize/page.tsx@@ -1,12 +1,14 @@import { getSession } from "@/lib/auth";import { vaidateAuthorizeRequest } from "@/lib/oauth";-import { prisma } from "@/lib/prisma";import z from "@/lib/zod";import { authorizeSchema } from "@/lib/zod/schemas/oauth";-import { Background, Footer, Nav } from "@dub/ui";-import { TimerOff } from "lucide-react";+import { Logo } from "@dub/ui";+import { HOME_DOMAIN } from "@dub/utils";+import { ArrowLeftRight } from "lucide-react";import { redirect } from "next/navigation";+import { Suspense } from "react";import { AuthorizeForm } from "./AuthorizeForm";+import { Scopes } from "./Scopes";export const runtime = "nodejs";@@ -30,38 +32,31 @@ export default async function Authorize({const { oAuthClient, request } = await vaidateAuthorizeRequest(searchParams);- const workspaces = await prisma.project.findMany({- where: {- users: {- some: {- userId: session.user.id,- },- },- },- select: {- id: true,- name: true,- logo: true,- },- });-return (- <main className="flex min-h-screen flex-col justify-between">- <Nav />- <div className="z-10 mx-2 my-10 flex max-w-md flex-col items-center space-y-5 px-2.5 text-center sm:mx-auto sm:max-w-lg sm:px-0 lg:mb-16">- <div className="mx-auto flex h-20 w-20 items-center justify-center rounded-full border border-gray-300 bg-white/30">- <TimerOff className="h-6 w-6 text-gray-400" />+ <div className="relative z-10 mt-[calc(30vh)] h-fit w-full max-w-md overflow-hidden border-y border-gray-200 sm:rounded-2xl sm:border sm:shadow-xl">+ <div className="flex flex-col items-center justify-center space-y-3 border-b border-gray-200 bg-white px-4 py-6 pt-8 text-center sm:px-16">+ <div className="flex items-center gap-3">+ <a href={oAuthClient.website} target="_blank" rel="noreferrer">+ <Logo className="h-10 w-10" />+ </a>+ <ArrowLeftRight className="h-6 w-6 text-gray-500" />+ <a href={HOME_DOMAIN} target="_blank" rel="noreferrer">+ <Logo className="h-10 w-10" />+ </a></div>- <h1 className="font-display text-5xl font-bold">{oAuthClient.name}</h1>- <p className="text-lg text-gray-600">{}</p>- <AuthorizeForm- oAuthClient={oAuthClient}- workspaces={workspaces}- {...request}- />+ <p className="text-md text-gray-500">+ <span className="font-bold">{oAuthClient.name}</span> is requesting+ API access to a workspace.+ </p>+ </div>+ <div className="flex flex-col space-y-3 bg-white px-2 py-6 sm:px-10">+ <Scopes scopes={oAuthClient.scopes} />+ </div>+ <div className="flex flex-col space-y-2 border-t border-gray-200 bg-white px-2 py-6 sm:px-10">+ <Suspense fallback={<></>}>+ <AuthorizeForm oAuthClient={oAuthClient} {...request} />+ </Suspense></div>- <Footer />- <Background />- </main>+ </div>);}diff --git a/apps/web/lib/oauth.ts b/apps/web/lib/oauth.tsindex fa62040f79..f4ce463285 100644--- a/apps/web/lib/oauth.ts+++ b/apps/web/lib/oauth.ts@@ -1,3 +1,5 @@+"use server";+import { prisma } from "@/lib/prisma";import { nanoid } from "@dub/utils";import { redirect } from "next/navigation";@@ -31,8 +33,6 @@ export const vaidateAuthorizeRequest = async (params: any) => {// Handle the authorize request and create a new OAuth codeexport const handleAuthorize = async (formData: FormData) => {- "use server";-const session = await getSession();if (!session) {