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 redirect

Kiran Kba49163341e3
diff --git a/apps/web/app/api/slack/callback/route.ts b/apps/web/app/api/slack/callback/route.ts
index 0179d771e0..854fc6572c 100644
--- a/apps/web/app/api/slack/callback/route.ts
+++ b/apps/web/app/api/slack/callback/route.ts
@@ -6,6 +6,7 @@ import { prisma } from "@/lib/prisma";
import { redis } from "@/lib/upstash";
import z from "@/lib/zod";
import { getSearchParams } from "@dub/utils";
+import { Project } from "@prisma/client";
import { redirect } from "next/navigation";
const schema = z.object({
@@ -14,68 +15,71 @@ const schema = z.object({
});
export const GET = async (req: Request) => {
- try {
-
- } catch(e: any) {
- //
- }
-
+ let workspace: Pick<Project, "slug"> | null = null;
- const session = await getSession();
-
- if (!session?.user.id) {
- return new Response("Unauthorized", { status: 401 });
- }
+ try {
+ const session = await getSession();
+
+ if (!session?.user.id) {
+ throw new DubApiError({
+ code: "unauthorized",
+ message: "Unauthorized",
+ });
+ }
+
+ const { code, state } = schema.parse(getSearchParams(req.url));
+
+ // Find workspace that initiated the Stripe app install
+ const workspaceId = await redis.get<string>(`slack:install:state:${state}`);
+
+ if (!workspaceId) {
+ throw new DubApiError({
+ code: "bad_request",
+ message: "Unknown state",
+ });
+ }
+
+ workspace = await prisma.project.findFirstOrThrow({
+ where: {
+ id: workspaceId,
+ },
+ select: {
+ slug: true,
+ },
+ });
- const { code, state } = schema.parse(getSearchParams(req.url));
+ 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}`);
- // Find workspace that initiated the Stripe app install
- const workspaceId = await redis.get<string>(`slack:install:state:${state}`);
+ const response = await fetch("https://slack.com/api/oauth.v2.access", {
+ method: "POST",
+ body: formData,
+ });
- if (!workspaceId) {
- throw new DubApiError({
- code: "bad_request",
- message: "Unknown state",
+ const data = await response.json();
+
+ const credentials: SlackCredential = {
+ appId: data.app_id,
+ botUserId: data.bot_user_id,
+ scope: data.scope,
+ accessToken: data.access_token,
+ tokenType: data.token_type,
+ authUser: data.authed_user,
+ team: data.team,
+ };
+
+ await installIntegration({
+ integrationSlug: "slack",
+ userId: session.user.id,
+ workspaceId,
+ credentials,
});
+ } catch (e: any) {
+ console.error(e);
+ redirect(`/integrations/slack?error=${e.message}`);
}
- const workspace = await prisma.project.findFirstOrThrow({
- where: {
- id: workspaceId,
- },
- select: {
- slug: true,
- },
- });
-
- 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}`);
-
- const response = await fetch("https://slack.com/api/oauth.v2.access", {
- method: "POST",
- body: formData,
- });
-
- const data = await response.json();
-
- const credentials: SlackCredential = {
- appId: data.app_id,
- botUserId: data.bot_user_id,
- scope: data.scope,
- accessToken: data.access_token,
- tokenType: data.token_type,
- authUser: data.authed_user,
- team: data.team,
- };
-
- await installIntegration({
- integrationSlug: "slack",
- userId: session.user.id,
- workspaceId,
- credentials,
- });
-
- redirect(`${workspace.slug}/integrations/slack`);
+ redirect(`${workspace?.slug}/integrations/slack`);
};