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.

3.9 KB
Raw Blame
import { validateAuthorizeRequest } from "@/lib/api/oauth/actions";
import { getSession } from "@/lib/auth";
import { authorizeRequestSchema } from "@/lib/zod/schemas/oauth";
import { Callout } from "@/ui/shared/callout";
import EmptyState from "@/ui/shared/empty-state";
import { BlurImage, Logo } from "@dub/ui";
import { CubeSettings } from "@dub/ui/icons";
import { constructMetadata } from "@dub/utils";
import { ArrowLeftRight } from "lucide-react";
import { redirect } from "next/navigation";
import { Suspense } from "react";
import * as z from "zod/v4";
import { AuthorizeForm } from "./authorize-form";
import { AuthorizeWorkspaceProvider } from "./authorize-workspace-context";
import { ScopesRequested } from "./scopes-requested";

export const metadata = constructMetadata({
  title: "Authorize API access | Dub",
  noIndex: true,
});

// OAuth app consent page
export default async function Authorize(props: {
  searchParams?: Promise<z.infer<typeof authorizeRequestSchema>>;
}) {
  const searchParams = await props.searchParams;
  const session = await getSession();

  if (!session) {
    redirect("/login");
  }

  const { error, integration, requestParams } =
    await validateAuthorizeRequest(searchParams);

  if (error || !integration) {
    return (
      <EmptyState
        icon={CubeSettings}
        title="Invalid OAuth Request"
        description={error}
      />
    );
  }

  return (
    <div className="relative z-10 mx-4 my-auto w-full max-w-md rounded-2xl border border-neutral-200 bg-white shadow-xl sm:mx-auto">
      <div className="flex flex-col items-center justify-center gap-3 border-b border-neutral-200 px-4 py-6 pt-8 text-center sm:rounded-t-2xl sm:px-16">
        <div className="flex items-center gap-3">
          <a href={integration.website} target="_blank" rel="noreferrer">
            {integration.logo ? (
              <BlurImage
                src={integration.logo}
                alt={`Logo for ${integration.name}`}
                className="size-12 rounded-full border border-neutral-200"
                width={20}
                height={20}
              />
            ) : (
              <Logo className="size-12" />
            )}
          </a>
          <ArrowLeftRight className="size-5 text-neutral-500" />
          <a href="https://dub.co" target="_blank" rel="noreferrer">
            <Logo className="size-12" />
          </a>
        </div>

        <p className="text-md">
          <span className="font-bold">{integration.name}</span> is requesting
          API access to a workspace on Dub.
        </p>
        <span className="text-xs text-neutral-500">
          Built by{" "}
          <a
            href={integration.website}
            target="_blank"
            rel="noreferrer"
            className="underline"
          >
            {integration.developer}
          </a>
        </span>

        {!integration.verified && (
          <Callout
            variant="warn"
            size={2}
            className="w-full text-left sm:-mx-6 sm:w-auto sm:self-stretch"
          >
            <div className="flex flex-col gap-0.5">
              <p className="font-medium">
                Dub hasn't verified this integration
              </p>
              <p className="text-amber-800">
                It can only be installed by its developer or on the developer's
                workspace.
              </p>
            </div>
          </Callout>
        )}
      </div>
      <AuthorizeWorkspaceProvider integration={integration}>
        <div className="flex flex-col space-y-3 px-4 py-6 sm:px-10">
          <ScopesRequested scopes={requestParams.scope} />
        </div>
        <div className="flex flex-col space-y-2 border-t border-neutral-200 px-4 py-6 sm:rounded-b-2xl sm:px-10">
          <Suspense>
            <AuthorizeForm {...requestParams} integration={integration} />
          </Suspense>
        </div>
      </AuthorizeWorkspaceProvider>
    </div>
  );
}