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.

add env check

Kiran Kd0f8501c35fe
diff --git a/apps/web/app/api/slack/callback/route.ts b/apps/web/app/api/slack/callback/route.ts
index 648b74a30b..73b1e240a2 100644
--- a/apps/web/app/api/slack/callback/route.ts
+++ b/apps/web/app/api/slack/callback/route.ts
@@ -1,6 +1,7 @@
import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
import { getSession } from "@/lib/auth";
import { installIntegration } from "@/lib/integrations/install";
+import { getSlackEnv } from "@/lib/integrations/slack/env";
import { SlackCredential } from "@/lib/integrations/slack/type";
import { prisma } from "@/lib/prisma";
import { redis } from "@/lib/upstash";
@@ -15,6 +16,8 @@ const oAuthCallbackSchema = z.object({
});
export const GET = async (req: Request) => {
+ const env = getSlackEnv();
+
let workspace: Pick<Project, "slug"> | null = null;
try {
@@ -50,8 +53,8 @@ export const GET = async (req: Request) => {
const formData = new FormData();
formData.append("code", code);
- formData.append("client_id", `${process.env.SLACK_CLIENT_ID}`);
- formData.append("client_secret", `${process.env.SLACK_CLIENT_SECRET}`);
+ formData.append("client_id", env.SLACK_CLIENT_ID);
+ formData.append("client_secret", env.SLACK_CLIENT_SECRET);
const response = await fetch("https://slack.com/api/oauth.v2.access", {
method: "POST",
@@ -60,6 +63,8 @@ export const GET = async (req: Request) => {
const data = await response.json();
+ console.log(data);
+
const credentials: SlackCredential = {
appId: data.app_id,
botUserId: data.bot_user_id,
diff --git a/apps/web/lib/integrations/slack/env.ts b/apps/web/lib/integrations/slack/env.ts
new file mode 100644
index 0000000000..420b451533
--- /dev/null
+++ b/apps/web/lib/integrations/slack/env.ts
@@ -0,0 +1,19 @@
+import z from "@/lib/zod";
+
+export const envSchema = z.object({
+ SLACK_APP_INSTALL_URL: z.string(),
+ SLACK_CLIENT_ID: z.string(),
+ SLACK_CLIENT_SECRET: z.string(),
+});
+
+export const getSlackEnv = () => {
+ const env = envSchema.safeParse(process.env);
+
+ if (!env.success) {
+ throw new Error(
+ "Slack app environment variables are not configured properly.",
+ );
+ }
+
+ return env.data;
+};
diff --git a/apps/web/lib/integrations/slack/install.ts b/apps/web/lib/integrations/slack/install.ts
index bdf2273693..2e237312c1 100644
--- a/apps/web/lib/integrations/slack/install.ts
+++ b/apps/web/lib/integrations/slack/install.ts
@@ -1,29 +1,17 @@
import { redis } from "@/lib/upstash";
-import z from "@/lib/zod";
import { APP_DOMAIN_WITH_NGROK, nanoid } from "@dub/utils";
-
-const envSchema = z.object({
- SLACK_APP_INSTALL_URL: z.string(),
- SLACK_CLIENT_ID: z.string(),
- SLACK_CLIENT_SECRET: z.string(),
-});
+import { getSlackEnv } from "./env";
// Get the installation URL for Slack
export const getSlackInstallationUrl = async (workspaceId: string) => {
+ const env = getSlackEnv();
+
const state = nanoid(16);
await redis.set(`slack:install:state:${state}`, workspaceId, {
ex: 30 * 60,
});
- const env = envSchema.safeParse(process.env);
-
- if (!env.success) {
- throw new Error(
- "Slack App environment variables are not configured properly.",
- );
- }
-
- const url = new URL(env.data.SLACK_APP_INSTALL_URL);
+ const url = new URL(env.SLACK_APP_INSTALL_URL);
url.searchParams.set(
"redirect_uri",
`${APP_DOMAIN_WITH_NGROK}/api/slack/callback`,
diff --git a/apps/web/lib/integrations/slack/uninstall.ts b/apps/web/lib/integrations/slack/uninstall.ts
index 2c3b34bb91..6d935afbf1 100644
--- a/apps/web/lib/integrations/slack/uninstall.ts
+++ b/apps/web/lib/integrations/slack/uninstall.ts
@@ -1,5 +1,6 @@
import { DubApiError } from "@/lib/api/errors";
import { InstalledIntegration } from "@prisma/client";
+import { getSlackEnv } from "./env";
import { SlackCredential } from "./type";
export const uninstallSlackIntegration = async ({
@@ -7,6 +8,8 @@ export const uninstallSlackIntegration = async ({
}: {
installation: InstalledIntegration;
}) => {
+ const env = getSlackEnv();
+
const credentials = installation.credentials as SlackCredential;
const response = await fetch("https://slack.com/api/apps.uninstall", {
@@ -16,8 +19,8 @@ export const uninstallSlackIntegration = async ({
},
body: new URLSearchParams({
token: credentials.accessToken,
- client_id: `${process.env.SLACK_CLIENT_ID}`,
- client_secret: `${process.env.SLACK_CLIENT_SECRET}`,
+ client_id: env.SLACK_CLIENT_SECRET,
+ client_secret: env.SLACK_CLIENT_SECRET,
}),
});