2021-06-10 23:37:58 +00:00
|
|
|
import type {NextApiRequest, NextApiResponse} from 'next';
|
2021-06-11 00:03:48 +00:00
|
|
|
import {getSession} from "next-auth/client";
|
2021-06-10 23:37:58 +00:00
|
|
|
|
|
|
|
const client_id = process.env.ZOOM_CLIENT_ID;
|
|
|
|
const client_secret = process.env.ZOOM_CLIENT_SECRET;
|
|
|
|
|
|
|
|
const scopes = ['meeting:write:admin', 'meeting:write', 'meeting:read:admin', 'meeting:read'];
|
|
|
|
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
const { code } = req.query;
|
2021-06-11 00:03:48 +00:00
|
|
|
|
2021-06-10 23:37:58 +00:00
|
|
|
// Check that user is authenticated
|
2021-06-11 00:03:48 +00:00
|
|
|
const session = await getSession({req: req});
|
2021-06-10 23:37:58 +00:00
|
|
|
|
|
|
|
if (!session) { res.status(401).json({message: 'You must be logged in to do this'}); return; }
|
|
|
|
|
2021-06-11 00:03:48 +00:00
|
|
|
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');
|
2021-06-10 23:37:58 +00:00
|
|
|
|
|
|
|
// Convert to token
|
2021-06-11 00:03:48 +00:00
|
|
|
const options = {
|
|
|
|
method: 'POST',
|
2021-06-11 00:08:47 +00:00
|
|
|
body: {
|
2021-06-11 00:03:48 +00:00
|
|
|
grant_type: 'authorization_code',
|
|
|
|
code,
|
|
|
|
redirect_uri: authUrl
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
Authorization: authHeader
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-11 00:07:02 +00:00
|
|
|
return new Promise( async (resolve, reject) => {
|
2021-06-11 00:08:47 +00:00
|
|
|
const result = await fetch('https://zoom.us/oauth/token', options)
|
|
|
|
.then(res => res.text());
|
2021-06-11 00:07:02 +00:00
|
|
|
console.log(result);
|
|
|
|
|
|
|
|
/*const credential = await prisma.credential.create({
|
|
|
|
data: {
|
|
|
|
type: 'google_calendar',
|
|
|
|
key: 'lel',
|
|
|
|
userId: session.user.id
|
|
|
|
}
|
|
|
|
});*/
|
|
|
|
|
|
|
|
res.redirect('/integrations');
|
|
|
|
resolve();
|
|
|
|
});
|
2021-06-10 23:37:58 +00:00
|
|
|
}
|