Skip to content

Scheduling infrastructure for absolutely everyone.

Very rudimentary development version of zoom auth #2

nicolasa2f1462f1089
diff --git a/pages/api/integrations/zoom/callback.ts b/pages/api/integrations/zoom/callback.ts
index 8195f60c7c1..175d294c53c 100644
--- a/pages/api/integrations/zoom/callback.ts
+++ b/pages/api/integrations/zoom/callback.ts
@@ -1,4 +1,6 @@
import type {NextApiRequest, NextApiResponse} from 'next';
+import {getSession} from "next-auth/client";
+import prisma from "../../../../lib/prisma";
const client_id = process.env.ZOOM_CLIENT_ID;
const client_secret = process.env.ZOOM_CLIENT_SECRET;
@@ -7,28 +9,45 @@ const scopes = ['meeting:write:admin', 'meeting:write', 'meeting:read:admin', 'm
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { code } = req.query;
- console.log(code);
+
// Check that user is authenticated
- /*const session = await getSession({req: req});
+ const session = await getSession({req: req});
if (!session) { res.status(401).json({message: 'You must be logged in to do this'}); return; }
- // TODO Init some sort of oAuth client here
+ const redirectUri = encodeURI(process.env.BASE_URL + '/api/integrations/zoom/callback');
+ const authUrl = 'https://zoom.us/oauth/authorize?response_type=code&client_id=' + client_id + '&redirect_uri=' + redirectUri;
+ const authHeader = 'Basic ' + Buffer.from(client_id + ':' + client_secret).toString('base64');
// Convert to token
- /*return new Promise( (resolve, reject) => oAuth2Client.getToken(code, async (err, token) => {
- if (err) return console.error('Error retrieving access token', err);
-
- const credential = await prisma.credential.create({
- data: {
- type: 'google_calendar',
- key: token,
- userId: session.user.id
- }
- });
-
- res.redirect('/integrations');
- resolve();
- }));*/
- res.redirect('/integrations');
+ const options = {
+ method: 'POST',
+ qs: {
+ grant_type: 'authorization_code',
+ code,
+ redirect_uri: authUrl
+ },
+ headers: {
+ Authorization: authHeader
+ }
+ };
+
+ return new Promise( (resolve, reject) => fetch('https://zoom.us/oauth/token', options)
+ .then((res) => res.text())
+ .then((text) => {
+ console.log(text);
+ const credential = await prisma.credential.create({
+ data: {
+ type: 'google_calendar',
+ key: 'lel',
+ userId: session.user.id
+ }
+ });
+
+ res.redirect('/integrations');
+ resolve();
+ })
+ .catch((err) => {
+ if (err) throw new Error(err);
+ }));
}
\ No newline at end of file