The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
refactor displaying requested scope
Kiran K2a3aa94ec0bc
diff --git a/apps/web/app/app.dub.co/(auth)/oauth/authorize/authorize-form.tsx b/apps/web/app/app.dub.co/(auth)/oauth/authorize/authorize-form.tsxindex 0802dcf7d4..03232e2943 100644--- a/apps/web/app/app.dub.co/(auth)/oauth/authorize/authorize-form.tsx+++ b/apps/web/app/app.dub.co/(auth)/oauth/authorize/authorize-form.tsx@@ -10,11 +10,11 @@ import { useMemo, useState } from "react";import { toast } from "sonner";interface AuthorizeFormProps extends z.infer<typeof authorizeRequestSchema> {- oAuthClient: Pick<OAuthClient, "name">;+ oAuthApp: Pick<OAuthClient, "name">;}export const AuthorizeForm = (props: AuthorizeFormProps) => {- const { oAuthClient, client_id, redirect_uri, response_type, state } = props;+ const { oAuthApp, client_id, redirect_uri, response_type, state } = props;const { workspaces } = useWorkspaces();const [submitting, setSubmitting] = useState(false);@@ -90,7 +90,7 @@ export const AuthorizeForm = (props: AuthorizeFormProps) => {disabled={submitting}/><Button- text={`Authorize ${oAuthClient.name}`}+ text={`Authorize ${oAuthApp.name}`}type="submit"loading={submitting}disabled={!selectedWorkspace}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 89b1366514..ff97e237eb 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@@ -28,13 +28,24 @@ export default async function Authorize({redirect("/login");}- const { oAuthClient, request } = await vaidateAuthorizeRequest(searchParams);+ const { error, oAuthApp, requestParams } =+ await vaidateAuthorizeRequest(searchParams);++ if (error || !oAuthApp) {+ return (+ <div className="relative z-10 mt-[calc(30vh)] h-fit w-full max-w-md overflow-hidden border-y sm:rounded-2xl sm:border sm:shadow-xl">+ <div className="flex flex-col items-center justify-center space-y-6 bg-white px-4 py-6 pt-8 text-center sm:px-16">+ <p>{error}</p>+ </div>+ </div>+ );+ }return (<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-6 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">+ <a href={oAuthApp.website} target="_blank" rel="noreferrer"><Logo className="h-14 w-14" /></a><ArrowLeftRight className="h-6 w-6 text-gray-500" />@@ -45,21 +56,21 @@ export default async function Authorize({<p className="text-md"><aclassName="font-bold text-blue-500"- href={oAuthClient.website}+ href={oAuthApp.website}target="_blank"rel="noreferrer">- {oAuthClient.name}+ {oAuthApp.name}</a>{" "}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">- <ScopesRequested scopes={oAuthClient.scopes} />+ <ScopesRequested scopesRequested={oAuthApp.scopes.split(" ")} /></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} />+ <AuthorizeForm oAuthApp={oAuthApp} {...requestParams} /></Suspense></div></div>diff --git a/apps/web/app/app.dub.co/(auth)/oauth/authorize/scopes-requested.tsx b/apps/web/app/app.dub.co/(auth)/oauth/authorize/scopes-requested.tsxindex 07ce3ae35b..ae2a7bc30c 100644--- a/apps/web/app/app.dub.co/(auth)/oauth/authorize/scopes-requested.tsx+++ b/apps/web/app/app.dub.co/(auth)/oauth/authorize/scopes-requested.tsx@@ -4,30 +4,39 @@ import { permissions } from "@/lib/api/tokens/scopes";import { Check } from "lucide-react";interface ScopesProps {- scopes: string | null; // space separated scopes+ scopesRequested: string[];}export const ScopesRequested = (props: ScopesProps) => {- const { scopes } = props;+ const { scopesRequested } = props;- const scopesArray = scopes?.split(" ") ?? [];-- const requestedScopes = permissions.filter((p) =>- scopesArray.includes(p.scope),+ const requestedPermissions = permissions.filter((p) =>+ scopesRequested.includes(p.scope),);+ requestedPermissions.forEach((permission) => {+ permission.description = permission.description.replace(+ "Write",+ "<strong className='font-medium'>Write</strong>",+ );++ permission.description = permission.description.replace(+ "Read",+ "<strong className='font-medium'>Read</strong>",+ );+ });+return (<><span className="text-gray-600">Grant permissions:</span><ul className="text-md space-y-1">- {requestedScopes.map((requestedScope) => {+ {requestedPermissions.map((permission) => {return (- <li- className="flex items-center gap-2 text-gray-600"- key={requestedScope.scope}- >+ <li className="flex items-center gap-2" key={permission.scope}><Check className="h-4 w-4 text-green-500" />- {requestedScope.description}+ <div+ dangerouslySetInnerHTML={{ __html: permission.description }}+ /></li>);})}diff --git a/apps/web/lib/oauth.ts b/apps/web/lib/oauth.tsindex b42c396234..98fe4a9251 100644--- a/apps/web/lib/oauth.ts+++ b/apps/web/lib/oauth.ts@@ -1,28 +1,43 @@"use server";import { prisma } from "@/lib/prisma";+import { fromZodError } from "./api/errors";import { authorizeRequestSchema } from "./zod/schemas/oauth";export const vaidateAuthorizeRequest = async (params: any) => {- const request = authorizeRequestSchema.parse(params);- const { client_id: clientId, redirect_uri: redirectUri } = request;+ const request = authorizeRequestSchema.safeParse(params);- const oAuthClient = await prisma.oAuthClient.findFirst({+ if (!request.success) {+ const formattedError = fromZodError(request.error);++ return {+ error: formattedError.error.message,+ };+ }++ const requestParams = request.data;+ const { client_id: clientId, redirect_uri: redirectUri } = requestParams;++ const oAuthApp = await prisma.oAuthClient.findFirst({where: {clientId,},});- if (!oAuthClient) {- throw new Error(`Could not find OAuth client with clientId ${clientId}`);+ if (!oAuthApp) {+ return {+ error: "Could not find OAuth application.",+ };}- if (oAuthClient.redirectUri !== redirectUri) {- throw new Error("Invalid redirect_uri parameter for the application.");+ if (oAuthApp.redirectUri !== redirectUri) {+ return {+ error: "Invalid redirect_uri parameter for the application.",+ };}return {- oAuthClient,- request,+ oAuthApp,+ requestParams,};};diff --git a/apps/web/ui/modals/add-edit-oauth-app-modal.tsx b/apps/web/ui/modals/add-edit-oauth-app-modal.tsxindex 589a417a42..133f6bd5ad 100644--- a/apps/web/ui/modals/add-edit-oauth-app-modal.tsx+++ b/apps/web/ui/modals/add-edit-oauth-app-modal.tsx@@ -30,11 +30,11 @@ import { toast } from "sonner";import { mutate } from "swr";const newApp: OAuthAppProps = {- name: "Supa",- developer: "Kiran",- website: "https://app.dub.co/acme/settings/tokens",+ name: "",+ developer: "",+ website: "",clientId: "",- redirectUri: "https://app.dub.co/acme/settings/tokens",+ redirectUri: "",scopes: [],};diff --git a/apps/web/ui/modals/oauth-app-created-modal.tsx b/apps/web/ui/modals/oauth-app-created-modal.tsxindex 7fdfcc4847..936757aef0 100644--- a/apps/web/ui/modals/oauth-app-created-modal.tsx+++ b/apps/web/ui/modals/oauth-app-created-modal.tsx@@ -52,7 +52,7 @@ function AppCreatedModal({Client Secret</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 text-nowrap">+ <p className="text-nowrap font-mono text-sm text-gray-500">{app.clientSecret}</p><div className="flex flex-col gap-2">