-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathauthorize.js
68 lines (57 loc) · 1.81 KB
/
authorize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import fetch from "node-fetch";
import { createClient } from "@supabase/supabase-js";
const supabase = createClient(
process.env.VITE_DATABASE_URL,
process.env.VITE_DATABASE_PUBLIC_KEY,
);
exports.handler = async function (event) {
const queryStringParameters = event.queryStringParameters;
if (!event.queryStringParameters) {
return {
statusCode: 404,
body: JSON.stringify({ message: "Authorization URL incorrect" }),
};
}
const code = queryStringParameters.code;
const clientId = queryStringParameters.client_id;
const teamId = queryStringParameters.team_id;
const redirectUrl = `https://miro.com/app-install-completed/?client_id=${clientId}&team_id=${teamId}`;
const url = `https://api.miro.com/v1/oauth/token?grant_type=authorization_code&client_id=${clientId}&client_secret=${process.env.MIRO_CLIENT_SECRET}&code=${code}&redirect_uri=${process.env.MIRO_REDIRECT_URI}`;
try {
const authorizationResponse = await fetch(url, {
method: "POST",
});
const result = await authorizationResponse.json();
const miro_access_token = result.access_token;
const miro_user_id = result.user_id;
const modifiedAtTime = new Date();
const { data, error } = await supabase.from("auth").upsert([
{
access_token: miro_access_token,
miroUserId: miro_user_id,
modified_at: modifiedAtTime,
},
]);
if (error) {
return {
statusCode: 400,
body: JSON.stringify({ message: error }),
};
}
if (data) {
return {
statusCode: 302,
headers: {
Location: redirectUrl,
"Cache-Control": "no-cache",
},
body: JSON.stringify({}),
};
}
} catch (error) {
return {
statusCode: 400,
body: JSON.stringify({ message: error }),
};
}
};