The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
improve integration installation latency
Steven Tey4e0a82f81a42
diff --git a/apps/web/app/api/oauth/token/exchange-code-for-token.ts b/apps/web/app/api/oauth/token/exchange-code-for-token.tsindex 3c0afe4bc1..7b7199135a 100644--- a/apps/web/app/api/oauth/token/exchange-code-for-token.ts+++ b/apps/web/app/api/oauth/token/exchange-code-for-token.ts@@ -7,7 +7,8 @@ import { generateRandomName } from "@/lib/names";import z from "@/lib/zod";import { authCodeExchangeSchema } from "@/lib/zod/schemas/oauth";import { prisma } from "@dub/prisma";-import { getCurrentPlan } from "@dub/utils";+import { PRO_PLAN } from "@dub/utils";+import { waitUntil } from "@vercel/functions";import { NextRequest } from "next/server";// Exchange authorization code with access token@@ -46,16 +47,33 @@ export const exchangeAuthCodeForToken = async (});}- const app = await prisma.oAuthApp.findFirst({- where: {- clientId,- },- select: {- integrationId: true,- pkce: true,- hashedClientSecret: true,- },- });+ const [app, accessCode] = await Promise.all([+ prisma.oAuthApp.findUnique({+ where: {+ clientId,+ },+ select: {+ integrationId: true,+ pkce: true,+ hashedClientSecret: true,+ },+ }),+ prisma.oAuthCode.findUnique({+ where: {+ code,+ },+ select: {+ clientId: true,+ userId: true,+ projectId: true,+ scopes: true,+ redirectUri: true,+ expiresAt: true,+ codeChallenge: true,+ codeChallengeMethod: true,+ },+ }),+ ]);if (!app || !app.integrationId) {throw new DubApiError({@@ -91,24 +109,7 @@ export const exchangeAuthCodeForToken = async (}}- // Now let's find the access code and validate it- const accessCode = await prisma.oAuthCode.findUnique({- where: {- code,- clientId,- },- select: {- userId: true,- projectId: true,- scopes: true,- redirectUri: true,- expiresAt: true,- codeChallenge: true,- codeChallengeMethod: true,- },- });-- if (!accessCode) {+ if (!accessCode || accessCode.clientId !== clientId) {throw new DubApiError({code: "unauthorized",message: "Invalid code",@@ -150,15 +151,6 @@ export const exchangeAuthCodeForToken = async (});}- const workspace = await prisma.project.findUniqueOrThrow({- where: {- id: accessCode.projectId,- },- select: {- plan: true,- },- });-const accessToken = createToken({length: OAUTH_CONFIG.ACCESS_TOKEN_LENGTH,prefix: OAUTH_CONFIG.ACCESS_TOKEN_PREFIX,@@ -189,14 +181,6 @@ export const exchangeAuthCodeForToken = async (installationId: installation.id,},}),-- // Delete the code after it's been used- prisma.oAuthCode.delete({- where: {- code,- },- }),-// Create the access token and refresh tokenprisma.restrictedToken.create({data: {@@ -205,7 +189,7 @@ export const exchangeAuthCodeForToken = async (partialKey: `${accessToken.slice(0, 3)}...${accessToken.slice(-4)}`,scopes,expires: accessTokenExpires,- rateLimit: getCurrentPlan(workspace.plan as string).limits.api,+ rateLimit: PRO_PLAN.limits.api,userId,projectId,installationId: installation.id,@@ -222,6 +206,15 @@ export const exchangeAuthCodeForToken = async (}),]);+ waitUntil(+ // Delete the code after it's been used+ prisma.oAuthCode.delete({+ where: {+ code,+ },+ }),+ );+// https://www.oauth.com/oauth2-servers/access-tokens/access-token-response/return {access_token: accessToken,