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.

some improvements

Kiran K38c8ce96d51e
diff --git a/apps/web/app/api/oauth/authorize/route.ts b/apps/web/app/api/oauth/authorize/route.ts
index 88f3d4a4d7..9f0f070c02 100644
--- a/apps/web/app/api/oauth/authorize/route.ts
+++ b/apps/web/app/api/oauth/authorize/route.ts
@@ -6,6 +6,7 @@ import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import { authorizeRequestSchema } from "@/lib/zod/schemas/oauth";
import { prisma } from "@dub/prisma";
+import { STRIPE_INTEGRATION_ID } from "@dub/utils";
import { NextResponse } from "next/server";
// POST /api/oauth/authorize - approve OAuth authorization request
@@ -40,9 +41,22 @@ export const POST = withWorkspace(async ({ session, req, workspace }) => {
select: {
redirectUris: true,
pkce: true,
+ integrationId: true,
},
});
+ // Special case for Stripe app: check if the workspace has conversionEnabled
+ if (
+ app.integrationId === STRIPE_INTEGRATION_ID &&
+ !workspace.conversionEnabled
+ ) {
+ throw new DubApiError({
+ code: "bad_request",
+ message:
+ "Stripe app can only be connected to a workspace with conversion enabled. Please contact us to get access.",
+ });
+ }
+
const redirectUris = (app.redirectUris || []) as string[];
if (!redirectUris.includes(redirectUri)) {
diff --git a/apps/web/app/api/oauth/token/route.ts b/apps/web/app/api/oauth/token/route.ts
index bf87df38f0..f3002febd7 100644
--- a/apps/web/app/api/oauth/token/route.ts
+++ b/apps/web/app/api/oauth/token/route.ts
@@ -29,7 +29,7 @@ export async function POST(req: NextRequest) {
});
}
} catch (error) {
- return handleAndReturnErrorResponse(error);
+ return handleAndReturnErrorResponse(error, CORS_HEADERS);
}
}
diff --git a/apps/web/app/api/oauth/userinfo/route.ts b/apps/web/app/api/oauth/userinfo/route.ts
index 23dad82ef0..6853ea81c2 100644
--- a/apps/web/app/api/oauth/userinfo/route.ts
+++ b/apps/web/app/api/oauth/userinfo/route.ts
@@ -70,7 +70,7 @@ export async function GET(req: NextRequest) {
headers: CORS_HEADERS,
});
} catch (e) {
- return handleAndReturnErrorResponse(e);
+ return handleAndReturnErrorResponse(e, CORS_HEADERS);
}
}
diff --git a/packages/stripe-app/src/hooks/use-workspace.ts b/packages/stripe-app/src/hooks/use-workspace.ts
index 4566738d2f..2d66203bbf 100644
--- a/packages/stripe-app/src/hooks/use-workspace.ts
+++ b/packages/stripe-app/src/hooks/use-workspace.ts
@@ -1,8 +1,9 @@
import { useCallback, useEffect, useState } from "react";
import Stripe from "stripe";
-import { fetchWorkspace } from "../utils/dub";
+import { getSecret } from "../utils/secrets";
import { Workspace } from "../utils/types";
+// Retrieve the workspace from the secrets
export const useWorkspace = (stripe: Stripe) => {
const [workspace, setWorkspace] = useState<Workspace | null>(null);
const [isLoading, setIsLoading] = useState(true);
@@ -24,3 +25,16 @@ export const useWorkspace = (stripe: Stripe) => {
mutate: loadWorkspace,
};
};
+
+async function fetchWorkspace({ stripe }: { stripe: Stripe }) {
+ const workspace = await getSecret({
+ stripe,
+ name: "dub_workspace",
+ });
+
+ if (!workspace) {
+ return null;
+ }
+
+ return JSON.parse(workspace) as Workspace;
+}
diff --git a/packages/stripe-app/src/utils/dub.ts b/packages/stripe-app/src/utils/dub.ts
index 28802483d8..2631991b3e 100644
--- a/packages/stripe-app/src/utils/dub.ts
+++ b/packages/stripe-app/src/utils/dub.ts
@@ -1,20 +1,16 @@
-import Stripe from "stripe";
import { DUB_HOST } from "./constants";
-import { getValidToken } from "./oauth";
-import { deleteSecret, getSecret } from "./secrets";
-import { Workspace } from "./types";
+import { Token } from "./types";
+// Update the workspace with stripeAccountId
export async function updateWorkspace({
- stripe,
+ token,
workspaceId,
accountId,
}: {
- stripe: Stripe;
+ token: Token;
workspaceId: string;
accountId: string | null;
}) {
- const token = await getValidToken({ stripe });
-
const response = await fetch(
`${DUB_HOST}/api/stripe/integration/callback?workspaceId=${workspaceId}`,
{
@@ -36,58 +32,3 @@ export async function updateWorkspace({
});
}
}
-
-export async function fetchWorkspace({ stripe }: { stripe: Stripe }) {
- const workspace = await getSecret({
- stripe,
- name: "dub_workspace",
- });
-
- if (!workspace) {
- return null;
- }
-
- return JSON.parse(workspace) as Workspace;
-}
-
-export async function connectWorkspace({
- stripe,
- workspaceId,
- accountId,
-}: {
- stripe: Stripe;
- workspaceId: string;
- accountId: string;
-}) {
- await updateWorkspace({
- stripe,
- workspaceId,
- accountId,
- });
-}
-
-export async function disconnectWorkspace({
- stripe,
- workspaceId,
-}: {
- stripe: Stripe;
- workspaceId: string;
-}) {
- await updateWorkspace({
- stripe,
- workspaceId,
- accountId: null,
- });
-
- await Promise.all([
- deleteSecret({
- stripe,
- name: "dub_token",
- }),
-
- deleteSecret({
- stripe,
- name: "dub_workspace",
- }),
- ]);
-}
diff --git a/packages/stripe-app/src/views/AppSettings.tsx b/packages/stripe-app/src/views/AppSettings.tsx
index 4e4892b71e..02650a7e23 100644
--- a/packages/stripe-app/src/views/AppSettings.tsx
+++ b/packages/stripe-app/src/views/AppSettings.tsx
@@ -15,9 +15,15 @@ import { useEffect, useRef, useState } from "react";
import Stripe from "stripe";
import { useWorkspace } from "../hooks/use-workspace";
import appIcon from "../icon.svg";
-import { connectWorkspace, disconnectWorkspace } from "../utils/dub";
-import { getOAuthUrl, getToken, getUserInfo } from "../utils/oauth";
-import { setSecret } from "../utils/secrets";
+import { updateWorkspace } from "../utils/dub";
+import {
+ getOAuthUrl,
+ getToken,
+ getUserInfo,
+ getValidToken,
+} from "../utils/oauth";
+import { deleteSecret, setSecret } from "../utils/secrets";
+import { Workspace } from "../utils/types";
// TODO:
// Handle errors and display them to the user
@@ -41,8 +47,43 @@ const AppSettings = ({ userContext, oauthContext }: ExtensionContextValue) => {
const verifier = oauthContext?.verifier;
const error = oauthContext?.error;
- // Verify token and connect workspace
- const verifyTokenAndConnectWorkspace = async () => {
+ // Disconnect workspace
+ const disconnectWorkspace = async ({
+ workspace,
+ }: {
+ workspace: Workspace;
+ }) => {
+ setDisconnecting(true);
+
+ const token = await getValidToken({ stripe });
+
+ await updateWorkspace({
+ token,
+ workspaceId: workspace.id,
+ accountId: null,
+ });
+
+ await Promise.all([
+ deleteSecret({
+ stripe,
+ name: "dub_workspace",
+ }),
+
+ await deleteSecret({
+ stripe,
+ name: "dub_token",
+ }),
+ ]);
+
+ mutate();
+ setDisconnecting(false);
+ };
+
+ // Exchange code for token
+ // Fetch the workspace info for the current user
+ // Connect the workspace to the stripe account
+ // Store the token and workspace info in the secrets
+ const connectWorkspace = async () => {
setConnecting(true);
if (!code || !verifier) {
@@ -67,8 +108,8 @@ const AppSettings = ({ userContext, oauthContext }: ExtensionContextValue) => {
return;
}
- await connectWorkspace({
- stripe,
+ await updateWorkspace({
+ token,
workspaceId: workspace.id,
accountId: userContext.account.id,
});
@@ -92,7 +133,7 @@ const AppSettings = ({ userContext, oauthContext }: ExtensionContextValue) => {
// there is an ongoing oauth flow
if (code && verifier && !workspace && !credentialsUsed.current) {
- verifyTokenAndConnectWorkspace();
+ connectWorkspace();
return;
}
@@ -123,8 +164,7 @@ const AppSettings = ({ userContext, oauthContext }: ExtensionContextValue) => {
setDisconnecting(true);
await disconnectWorkspace({
- stripe,
- workspaceId: workspace.id,
+ workspace,
});
mutate();