2021-09-22 19:52:38 +00:00
|
|
|
import { google } from "googleapis";
|
2021-08-18 11:52:25 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2023-01-10 02:01:57 +00:00
|
|
|
import { WEBAPP_URL, CAL_URL } from "@calcom/lib/constants";
|
2022-04-27 14:28:36 +00:00
|
|
|
import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl";
|
2022-03-23 22:00:30 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2022-03-23 22:00:30 +00:00
|
|
|
import { decodeOAuthState } from "../../_utils/decodeOAuthState";
|
2022-05-02 20:39:35 +00:00
|
|
|
import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug";
|
2022-09-15 19:53:09 +00:00
|
|
|
import getInstalledAppPath from "../../_utils/getInstalledAppPath";
|
2021-11-03 10:47:52 +00:00
|
|
|
|
2022-05-02 20:39:35 +00:00
|
|
|
let client_id = "";
|
|
|
|
let client_secret = "";
|
2021-03-26 15:51:19 +00:00
|
|
|
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
2021-08-19 12:27:01 +00:00
|
|
|
const { code } = req.query;
|
2023-01-10 02:01:57 +00:00
|
|
|
const state = decodeOAuthState(req);
|
|
|
|
|
2022-01-06 22:06:31 +00:00
|
|
|
if (code && typeof code !== "string") {
|
2021-08-19 12:27:01 +00:00
|
|
|
res.status(400).json({ message: "`code` must be a string" });
|
|
|
|
return;
|
|
|
|
}
|
2022-05-02 20:39:35 +00:00
|
|
|
if (!req.session?.user?.id) {
|
|
|
|
return res.status(401).json({ message: "You must be logged in to do this" });
|
2021-10-12 09:35:44 +00:00
|
|
|
}
|
2022-05-02 20:39:35 +00:00
|
|
|
|
|
|
|
const appKeys = await getAppKeysFromSlug("google-calendar");
|
|
|
|
if (typeof appKeys.client_id === "string") client_id = appKeys.client_id;
|
|
|
|
if (typeof appKeys.client_secret === "string") client_secret = appKeys.client_secret;
|
|
|
|
if (!client_id) return res.status(400).json({ message: "Google client_id missing." });
|
|
|
|
if (!client_secret) return res.status(400).json({ message: "Google client_secret missing." });
|
|
|
|
|
2022-04-04 20:26:14 +00:00
|
|
|
const redirect_uri = WEBAPP_URL + "/api/integrations/googlecalendar/callback";
|
2022-01-06 22:06:31 +00:00
|
|
|
|
2021-10-12 09:35:44 +00:00
|
|
|
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uri);
|
2021-11-03 10:47:52 +00:00
|
|
|
|
2022-01-06 22:06:31 +00:00
|
|
|
let key = "";
|
|
|
|
|
|
|
|
if (code) {
|
|
|
|
const token = await oAuth2Client.getToken(code);
|
|
|
|
key = token.res?.data;
|
|
|
|
}
|
|
|
|
|
2021-08-19 12:27:01 +00:00
|
|
|
await prisma.credential.create({
|
|
|
|
data: {
|
|
|
|
type: "google_calendar",
|
2021-08-19 14:37:18 +00:00
|
|
|
key,
|
2022-05-02 20:39:35 +00:00
|
|
|
userId: req.session.user.id,
|
|
|
|
appId: "google-calendar",
|
2021-08-19 12:27:01 +00:00
|
|
|
},
|
|
|
|
});
|
2023-01-10 02:01:57 +00:00
|
|
|
|
|
|
|
if (state?.installGoogleVideo) {
|
|
|
|
const existingGoogleMeetCredential = await prisma.credential.findFirst({
|
|
|
|
where: {
|
|
|
|
userId: req.session.user.id,
|
|
|
|
type: "google_video",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!existingGoogleMeetCredential) {
|
|
|
|
await prisma.credential.create({
|
|
|
|
data: {
|
|
|
|
type: "google_video",
|
|
|
|
key: {},
|
|
|
|
userId: req.session.user.id,
|
|
|
|
appId: "google-meet",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
res.redirect(
|
|
|
|
getSafeRedirectUrl(CAL_URL + "/apps/installed/conferencing?hl=google-meet") ??
|
|
|
|
getInstalledAppPath({ variant: "conferencing", slug: "google-meet" })
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2022-09-15 19:53:09 +00:00
|
|
|
res.redirect(
|
|
|
|
getSafeRedirectUrl(state?.returnTo) ??
|
|
|
|
getInstalledAppPath({ variant: "calendar", slug: "google-calendar" })
|
|
|
|
);
|
2021-08-19 12:27:01 +00:00
|
|
|
}
|