2021-08-19 12:27:01 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2022-05-02 23:13:34 +00:00
|
|
|
import { WEBAPP_URL } from "@calcom/lib/constants";
|
2022-03-23 22:00:30 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2021-06-10 23:37:58 +00:00
|
|
|
|
2022-06-11 22:15:29 +00:00
|
|
|
import { getZoomAppKeys } from "../lib";
|
2021-06-10 23:37:58 +00:00
|
|
|
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
2021-08-19 12:27:01 +00:00
|
|
|
const { code } = req.query;
|
2022-06-11 22:15:29 +00:00
|
|
|
const { client_id, client_secret } = await getZoomAppKeys();
|
2022-05-02 23:13:34 +00:00
|
|
|
|
|
|
|
const redirectUri = encodeURI(WEBAPP_URL + "/api/integrations/zoomvideo/callback");
|
2021-08-19 12:27:01 +00:00
|
|
|
const authHeader = "Basic " + Buffer.from(client_id + ":" + client_secret).toString("base64");
|
|
|
|
const result = await fetch(
|
|
|
|
"https://zoom.us/oauth/token?grant_type=authorization_code&code=" + code + "&redirect_uri=" + redirectUri,
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
Authorization: authHeader,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2021-12-15 14:02:39 +00:00
|
|
|
|
|
|
|
const responseBody = await result.json();
|
|
|
|
|
|
|
|
responseBody.expiry_date = Math.round(Date.now() + responseBody.expires_in * 1000);
|
|
|
|
delete responseBody.expires_in;
|
2021-08-19 12:27:01 +00:00
|
|
|
|
2021-12-10 21:14:54 +00:00
|
|
|
await prisma.user.update({
|
|
|
|
where: {
|
2022-03-23 22:00:30 +00:00
|
|
|
id: req.session?.user.id,
|
2021-12-10 21:14:54 +00:00
|
|
|
},
|
2021-08-19 12:27:01 +00:00
|
|
|
data: {
|
2021-12-10 21:14:54 +00:00
|
|
|
credentials: {
|
|
|
|
create: {
|
|
|
|
type: "zoom_video",
|
2021-12-15 14:02:39 +00:00
|
|
|
key: responseBody,
|
2021-12-10 21:14:54 +00:00
|
|
|
},
|
|
|
|
},
|
2021-08-19 12:27:01 +00:00
|
|
|
},
|
|
|
|
});
|
2021-12-10 21:14:54 +00:00
|
|
|
|
2022-03-23 22:00:30 +00:00
|
|
|
res.redirect("/apps/installed");
|
2021-08-19 12:27:01 +00:00
|
|
|
}
|