-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.js
128 lines (115 loc) · 3.49 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
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
var google = require('googleapis').google;
var sheets = google.sheets('v4');
var userName;
var dataDeets;
// the process.env values are set in .env
var clientID = process.env.CLIENT_ID;
var clientSecret = process.env.CLIENT_SECRET;
var callbackURL = 'https://'+process.env.PROJECT_DOMAIN+'.glitch.me/login/google/return';
var scopes = ['https://www.googleapis.com/auth/spreadsheets'];
var oauth2Client = new google.auth.OAuth2(clientID, clientSecret, callbackURL);
var url = oauth2Client.generateAuthUrl({
// 'online' (default) or 'offline' (gets refresh_token)
access_type: 'offline',
// If you only need one scope you can pass it as a string
scope: scopes,
// Force getting a refresh token
prompt: 'consent'
});
// init project
var express = require('express');
var app = express();
var expressSession = require('express-session');
// cookies are used to save authentication
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
app.use(express.static('views'))
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(expressSession({ secret:'watchingmonkeys', resave: true, saveUninitialized: true }));
if(process.env.REFRESH_TOKEN) {
oauth2Client.setCredentials({
refresh_token: process.env.REFRESH_TOKEN
});
}
// index route
app.get('/', function(req, res) {
res.sendFile(__dirname + '/views/index.html');
});
// on clicking "logoff" the cookie is cleared
app.get('/logoff',
function(req, res) {
res.clearCookie('google-auth');
res.redirect('/');
}
);
app.get('/auth/google', function(req, res) {
res.redirect(url);
});
app.get('/login/google/return', function(req, res) {
oauth2Client.getToken(req.query.code, function (err, tokens) {
console.log(tokens)
// Tokens contains an access_token and a refresh_token if you set access type to offline. Save them.
if (!err) {
oauth2Client.setCredentials({
access_token: tokens.access_token
});
res.redirect('/setcookie');
} else {
console.log("Aww, man: " + err);
}
});
}
);
// on successful auth, a cookie is set before redirecting
// to the success view
app.get('/setcookie',
function(req, res) {
res.cookie('google-auth', new Date());
res.redirect('/success');
}
);
// if cookie exists, success. otherwise, user is redirected to index
app.get('/success',
function(req, res) {
if(req.cookies['google-auth']) {
res.sendFile(__dirname + '/views/success.html');
} else {
res.redirect('/');
}
}
);
app.post('/spreadsheet', function(req, res) {
var body = req.body;
if(body.secret_key !== process.env.SECRET_KEY) {
return res.status(403).send("Incorrect secret key");
}
if(!body.data) {
return res.status(400).send("No data included");
}
if(!body.spreadsheet_key) {
return res.status(400).send("No spreadsheet key included!");
}
sheets.spreadsheets.values.append({
spreadsheetId: body.spreadsheet_key,
range: body.spreadsheet_range || 'Sheet1',
valueInputOption: 'RAW',
insertDataOption: 'INSERT_ROWS',
resource: {
values: [
body.data
],
},
auth: oauth2Client
}, (err, response) => {
if (err) {
res.status(500).send(err);
return console.error(err);
}
return res.status(200).send(response);
})
});
var listener = app.listen(process.env.PORT, function() {
console.log('Your app is listening on port ' + listener.address().port);
});