-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
128 lines (117 loc) · 3.01 KB
/
handler.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const { google } = require("googleapis");
const OAuth2 = google.auth.OAuth2;
const calendar = google.calendar("v3");
const SCOPES = ["https://www.googleapis.com/auth/calendar.readonly"];
const credentials = {
client_id: process.env.CLIENT_ID,
project_id: process.env.PROJECT_ID,
client_secret: process.env.CLIENT_SECRET,
calendar_id: process.env.CALENDAR_ID,
auth_uri: "https://accounts.google.com/o/oauth2/auth",
token_uri: "https://oauth2.googleapis.com/token",
auth_provider_x509_cert_url: "https://www.googleapis.com/oauth2/v1/certs",
redirect_uris: ["https://evandersloot.github.io/meet-app/"],
javascript_origins: ["https://evandersloot.github.io", "http://localhost:3000"],
};
const { client_secret, client_id, redirect_uris, calendar_id } = credentials;
const oAuth2Client = new google.auth.OAuth2(
client_id,
client_secret,
redirect_uris[0]
);
module.exports.getAuthURL = async () => {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: "offline",
scope: SCOPES,
});
return {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
authUrl: authUrl,
}),
};
};
module.exports.getAccessToken = async (event) => {
const oAuth2Client = new google.auth.OAuth2(
client_id,
client_secret,
redirect_uris[0]
);
const code = decodeURIComponent(`${event.pathParameters.code}`);
return new Promise((resolve, reject) => {
oAuth2Client.getToken(code, (err, token) => {
if (err) {
return reject(err);
}
return resolve(token);
});
})
.then((token) => {
return {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify(token),
};
})
.catch((err) => {
console.error(err);
return {
statusCode: 500,
headers: {
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify(err),
};
});
};
module.exports.getCalendarEvents = async (event) => {
const oAuth2Client = new google.auth.OAuth2(
client_id,
client_secret,
redirect_uris[0],
);
const access_token = decodeURIComponent(`${event.pathParameters.access_token}`);
oAuth2Client.setCredentials({ access_token });
return new Promise((resolve, reject) => {
calendar.events.list(
{
calendarId: calendar_id,
auth: oAuth2Client,
timeMin: new Date().toISOString(),
singleEvents: true,
orderBy: "startTime",
},
(error, response) => {
if (error) {
reject(error);
} else {
resolve(response);
}
}
);
})
.then((results) => {
return {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({ events: results.data.items }),
};
})
.catch((err) => {
console.error(err);
return {
statusCode: 500,
headers: {
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify(err),
};
});
};