forked from sitepoint-editors/google-calendar-twilio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
64 lines (46 loc) · 1.3 KB
/
server.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
var google = require('./common/google');
var connection = require('./common/db');
var express = require('express');
var app = express();
app.get('/', function(req, res){
var url = google.oauth2Client.generateAuthUrl({
access_type: google.config.access_type,
scope: google.config.scopes
});
res.send('<a href="' + url + '">login to google</a>');
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
function updateAccessToken(tokens, response){
connection.db.query(
"UPDATE users SET access_token = ? WHERE id = 1",
[JSON.stringify(tokens)],
function(err, rows, fields){
if(!err){
console.log('updated!');
response.send('connected!');
}else{
console.log('error updating table');
console.log(err);
response.send('error occured, please try again');
}
}
);
}
app.get('/login', function(req, res){
var code = req.query.code;
console.log('login');
google.oauth2Client.getToken(code, function(err, tokens){
if(!err){
console.log('tokens');
console.log(tokens);
updateAccessToken(tokens, res);
}else{
res.send('error getting token');
console.log('error getting token');
}
});
});