cal.pub0.org/packages/app-store/office365calendar/api/add.ts

31 lines
1.1 KiB
TypeScript
Raw Normal View History

import type { NextApiRequest, NextApiResponse } from "next";
import { stringify } from "querystring";
2022-05-02 23:13:34 +00:00
import { WEBAPP_URL } from "@calcom/lib/constants";
2022-05-02 23:13:34 +00:00
import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug";
import { encodeOAuthState } from "../../_utils/oauth/encodeOAuthState";
const scopes = ["User.Read", "Calendars.Read", "Calendars.ReadWrite", "offline_access"];
2022-05-02 23:13:34 +00:00
let client_id = "";
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === "GET") {
2022-05-02 23:13:34 +00:00
const appKeys = await getAppKeysFromSlug("office365-calendar");
if (typeof appKeys.client_id === "string") client_id = appKeys.client_id;
if (!client_id) return res.status(400).json({ message: "Office 365 client_id missing." });
const state = encodeOAuthState(req);
const params = {
response_type: "code",
scope: scopes.join(" "),
2022-05-02 23:13:34 +00:00
client_id,
redirect_uri: WEBAPP_URL + "/api/integrations/office365calendar/callback",
state,
};
const query = stringify(params);
const url = `https://login.microsoftonline.com/common/oauth2/v2.0/authorize?${query}`;
res.status(200).json({ url });
}
}