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.

Uninstall Dub app from HubSpot when integration is removed

Kiran Ka318b996bff9
diff --git a/apps/web/app/api/integrations/uninstall/route.ts b/apps/web/app/api/integrations/uninstall/route.ts
index 8fb4c89e34..6ed8f2ce83 100644
--- a/apps/web/app/api/integrations/uninstall/route.ts
+++ b/apps/web/app/api/integrations/uninstall/route.ts
@@ -1,9 +1,14 @@
import { DubApiError } from "@/lib/api/errors";
import { withWorkspace } from "@/lib/auth";
import { googleAdsInstalledWorkspaces } from "@/lib/integrations/google-ads/installed-workspaces";
+import { hubSpotOAuthProvider } from "@/lib/integrations/hubspot/oauth";
import { slackOAuthProvider } from "@/lib/integrations/slack/oauth";
import { prisma } from "@/lib/prisma";
-import { GOOGLE_ADS_INTEGRATION_ID, SLACK_INTEGRATION_ID } from "@dub/utils";
+import {
+ GOOGLE_ADS_INTEGRATION_ID,
+ HUBSPOT_INTEGRATION_ID,
+ SLACK_INTEGRATION_ID,
+} from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
@@ -58,6 +63,9 @@ export const DELETE = withWorkspace(
...(integrationId === GOOGLE_ADS_INTEGRATION_ID
? [googleAdsInstalledWorkspaces.remove(workspace.id)]
: []),
+ ...(integrationId === HUBSPOT_INTEGRATION_ID
+ ? [hubSpotOAuthProvider.uninstall(installation)]
+ : []),
]),
);
diff --git a/apps/web/lib/integrations/hubspot/oauth.ts b/apps/web/lib/integrations/hubspot/oauth.ts
index 64e619dab6..64d6df1575 100644
--- a/apps/web/lib/integrations/hubspot/oauth.ts
+++ b/apps/web/lib/integrations/hubspot/oauth.ts
@@ -51,6 +51,40 @@ class HubSpotOAuthProvider extends OAuthProvider<
return credentials;
}
+ async uninstall(installation: InstalledIntegration) {
+ const token = hubSpotAuthTokenSchema.parse(installation.credentials);
+
+ let accessToken = decryptOrPassthrough(token.access_token);
+
+ if (!this.isTokenValid(token)) {
+ const newToken = await this.refreshToken(
+ decryptOrPassthrough(token.refresh_token),
+ );
+
+ accessToken = newToken.access_token;
+ }
+
+ const response = await fetch(
+ "https://api.hubapi.com/appinstalls/2026-03/external-install",
+ {
+ method: "DELETE",
+ headers: {
+ Authorization: `Bearer ${accessToken}`,
+ },
+ },
+ );
+
+ if (!response.ok) {
+ const data = await response.text();
+
+ console.error("[HubSpot] uninstall", data);
+
+ throw new Error(
+ `[HubSpot] Failed to uninstall the app from HubSpot portal ${token.hub_id}.`,
+ );
+ }
+ }
+
isTokenValid(token: HubSpotAuthToken) {
if (!token.created_at) {
return false;