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.

fix validation and display date

Kiran Kc270ca422e21
diff --git a/apps/web/app/api/oauth-apps/authorized/route.ts b/apps/web/app/api/oauth-apps/authorized/route.ts
index 8ae541f3ce..dc626b4607 100644
--- a/apps/web/app/api/oauth-apps/authorized/route.ts
+++ b/apps/web/app/api/oauth-apps/authorized/route.ts
@@ -1,5 +1,7 @@
import { withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
+import z from "@/lib/zod";
+import { authorizedAppSchema } from "@/lib/zod/schemas/oauth";
import { NextResponse } from "next/server";
// GET /api/oauth-apps/authorized - applications authorized to access workspace resources
@@ -23,9 +25,9 @@ export const GET = withWorkspace(
},
});
- console.log(appsAuthorized);
-
- return NextResponse.json(appsAuthorized);
+ return NextResponse.json(
+ z.array(authorizedAppSchema).parse(appsAuthorized),
+ );
},
{
requiredScopes: ["workspaces.read"],
diff --git a/apps/web/app/api/oauth/authorize/route.ts b/apps/web/app/api/oauth/authorize/route.ts
index 35f590106a..6e63cf259e 100644
--- a/apps/web/app/api/oauth/authorize/route.ts
+++ b/apps/web/app/api/oauth/authorize/route.ts
@@ -1,18 +1,15 @@
import { TOKEN_EXPIRY, TOKEN_LENGTH } from "@/lib/api/oauth";
import { parseRequestBody } from "@/lib/api/utils";
-import { withSession } from "@/lib/auth";
+import { withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
-import { approveAuthorizeRequestSchema } from "@/lib/zod/schemas/oauth";
+import { authorizeRequestSchema } from "@/lib/zod/schemas/oauth";
import { nanoid } from "@dub/utils";
import { NextResponse } from "next/server";
// POST /api/oauth/authorize - approve OAuth authorization request
-export const POST = withSession(async ({ session, req }) => {
- const body = approveAuthorizeRequestSchema.parse(await parseRequestBody(req));
+export const POST = withWorkspace(async ({ session, req, workspace }) => {
+ const body = authorizeRequestSchema.parse(await parseRequestBody(req));
const { state, client_id: clientId, redirect_uri: redirectUri } = body;
- let { workspaceId } = body;
-
- workspaceId = workspaceId.replace("ws_", "");
const oAuthClient = await prisma.oAuthClient.findUniqueOrThrow({
where: {
@@ -20,14 +17,8 @@ export const POST = withSession(async ({ session, req }) => {
},
});
- await prisma.project.findUniqueOrThrow({
- where: {
- id: workspaceId,
- },
- });
-
console.info("Authorize request", {
- workspaceId,
+ workspaceId: workspace.id,
state,
clientId,
redirectUri,
@@ -37,7 +28,7 @@ export const POST = withSession(async ({ session, req }) => {
data: {
clientId,
redirectUri,
- projectId: workspaceId,
+ projectId: workspace.id,
userId: session.user.id,
scopes: oAuthClient.scopes,
code: nanoid(TOKEN_LENGTH.code),
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.tsx
index 631c06b5ad..5d131aa64c 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
@@ -46,10 +46,13 @@ export const AuthorizeForm = (props: AuthorizeFormProps) => {
e.preventDefault();
setSubmitting(true);
- const response = await fetch("/api/oauth/authorize", {
- method: "POST",
- body: JSON.stringify(Object.fromEntries(new FormData(e.currentTarget))),
- });
+ const response = await fetch(
+ `/api/oauth/authorize?workspaceId=${selectedWorkspace?.id}`,
+ {
+ method: "POST",
+ body: JSON.stringify(Object.fromEntries(new FormData(e.currentTarget))),
+ },
+ );
const data = await response.json();
@@ -68,7 +71,6 @@ 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} />
- <input type="hidden" name="workspaceId" value={selectedWorkspace?.id} />
<p className="text-sm text-gray-500">
Select a workspace to grant API access to
</p>
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
index 99292226d8..69a9f54e5d 100644
--- 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
@@ -71,7 +71,7 @@ const AppRow = (app: OAuthAuthorizedAppProps) => {
<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
+ {app.createdAt!}
</p>
</div>
</div>
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index a79a96ef56..e573444fd2 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -198,4 +198,6 @@ export type TokenProps = z.infer<typeof tokenSchema>;
export type OAuthAppProps = z.infer<typeof oAuthClientSchema>;
-export type OAuthAuthorizedAppProps = z.infer<typeof authorizedAppSchema>;
+export type OAuthAuthorizedAppProps = z.infer<typeof authorizedAppSchema> & {
+ createdAt: string;
+};
diff --git a/apps/web/lib/zod/schemas/oauth.ts b/apps/web/lib/zod/schemas/oauth.ts
index b07f946806..5792b5e1c2 100644
--- a/apps/web/lib/zod/schemas/oauth.ts
+++ b/apps/web/lib/zod/schemas/oauth.ts
@@ -57,13 +57,6 @@ export const authorizeRequestSchema = z.object({
state: z.string().max(255).optional(),
});
-// Aprove OAuth2.0 Authorization request
-export const approveAuthorizeRequestSchema = authorizeRequestSchema.extend({
- workspaceId: z
- .string()
- .min(1, "Please select a workspace to authorize the app"),
-});
-
// Schema for OAuth2.0 code exchange request
export const authCodeExchangeSchema = z.object({
grant_type: z.literal("authorization_code"),
@@ -91,11 +84,16 @@ export const tokenGrantSchema = z.discriminatedUnion(
},
);
-export const authorizedAppSchema = oAuthClientSchema.pick({
- clientId: true,
- name: true,
- developer: true,
- website: true,
- scopes: true,
- createdAt: true,
-});
+export const authorizedAppSchema = oAuthClientSchema
+ .pick({
+ clientId: true,
+ name: true,
+ developer: true,
+ website: true,
+ scopes: true,
+ })
+ .merge(
+ z.object({
+ createdAt: z.date(),
+ }),
+ );