-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
134 lines (123 loc) · 3.59 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
129
130
131
132
133
134
const { google } = require("googleapis");
const OAuth2 = google.auth.OAuth2;
const calendar = google.calendar("v3");
/**
* SCOPES allows you to set access levels; this is set to read only for now because you don"t have access rights to
* update the calendar yourself. For more info, check out the SCOPES documentation at this link: https://developers.google.com/identity/protocols/oauth2/scopes
*/
const SCOPES = ["https://www.googleapis.com/auth/calendar.readonly"];
/**
* Credentials are values required to get access to your calendar. If you see “process.env” this means
* the value is in the “config.json” file. This is a best practice as it keeps your API secrets hidden. Please remember to add “config.json” to your “.gitignore” file.
*/
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://ejquezada.github.io/Meet/"],
javascript_origins: ["https://ejquezada.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': '*',
"Access-Control-Allow-Credentials": true,
},
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': '*',
'Access-Control-Allow-Credentials': true,
},
body: JSON.stringify(token),
};
})
.catch((err) => {
console.error(err);
return {
statusCode: 500,
body: JSON.stringify(err),
};
});
};
module.exports.getCalendarEvents = 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( error => {
return {
statusCode: 500,
headers: {
'Access-Control-Allow-Origin': '*',
},
body: JSON.stringify(error),
};
});
}