* Upgrade jsforce to 3.6.2 * Refactor connecting to Salesforce * Revert yarn.lock changes * Migrate callback endpoint * Add jsforce node dependency * Migrate add endpoint * Import * Import package into crmService * Use new package types * Type fix * Update vite config * Push updated lockfile * Attempt to bump platform/libraries to unlock jsforce * Also update lockfile, naturally * bump platform libraries * Update vite.config * Add jsforce to vite config * fixup! Merge branch 'main' into upgrade-jsforce-v3 * fixup! fixup! Merge branch 'main' into upgrade-jsforce-v3 --------- Co-authored-by: Alex van Andel <me@alexvanandel.com> Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com> Co-authored-by: Morgan Vernay <morgan@cal.com> Co-authored-by: Omar López <zomars@me.com> Co-authored-by: Anik Dhabal Babu <81948346+anikdhabal@users.noreply.github.com>
51 lines
2.0 KiB
TypeScript
51 lines
2.0 KiB
TypeScript
import jsforce from "@jsforce/jsforce-node";
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import { WEBAPP_URL_FOR_OAUTH } from "@calcom/lib/constants";
|
|
import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl";
|
|
|
|
import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug";
|
|
import getInstalledAppPath from "../../_utils/getInstalledAppPath";
|
|
import createOAuthAppCredential from "../../_utils/oauth/createOAuthAppCredential";
|
|
import { decodeOAuthState } from "../../_utils/oauth/decodeOAuthState";
|
|
import appConfig from "../config.json";
|
|
|
|
let consumer_key = "";
|
|
let consumer_secret = "";
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
const { code } = req.query;
|
|
const state = decodeOAuthState(req);
|
|
|
|
if (code === undefined && typeof code !== "string") {
|
|
res.status(400).json({ message: "`code` must be a string" });
|
|
return;
|
|
}
|
|
|
|
if (!req.session?.user?.id) {
|
|
return res.status(401).json({ message: "You must be logged in to do this" });
|
|
}
|
|
|
|
const appKeys = await getAppKeysFromSlug("salesforce");
|
|
if (typeof appKeys.consumer_key === "string") consumer_key = appKeys.consumer_key;
|
|
if (typeof appKeys.consumer_secret === "string") consumer_secret = appKeys.consumer_secret;
|
|
if (!consumer_key) return res.status(400).json({ message: "Salesforce consumer key missing." });
|
|
if (!consumer_secret) return res.status(400).json({ message: "Salesforce consumer secret missing." });
|
|
|
|
const conn = new jsforce.Connection({
|
|
oauth2: {
|
|
clientId: consumer_key,
|
|
clientSecret: consumer_secret,
|
|
redirectUri: `${WEBAPP_URL_FOR_OAUTH}/api/integrations/salesforce/callback`,
|
|
},
|
|
});
|
|
|
|
const salesforceTokenInfo = await conn.oauth2.requestToken(code as string);
|
|
|
|
await createOAuthAppCredential({ appId: appConfig.slug, type: appConfig.type }, salesforceTokenInfo, req);
|
|
|
|
res.redirect(
|
|
getSafeRedirectUrl(state?.returnTo) ?? getInstalledAppPath({ variant: "other", slug: "salesforce" })
|
|
);
|
|
}
|