forked from ishantgupta777/spree2k20Final
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
109 lines (92 loc) · 2.64 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
const express = require('express')
const cors = require('cors')
const app = express()
const path = require('path')
const {
google
} = require('googleapis');
const keys = require('./key.json');
app.use(express.static('./client/build'))
app.use(express.json())
app.use(cors())
var sheet = google.sheets('v4');
const port = process.env.PORT || 5000
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
})
app.post('/register', (req, res) => {
const {
name,
email,
phone,
city,
college,
year,
gender,
selectedSports
} = req.body
authorize(async function (authClient) {
var request2 = {
spreadsheetId: '1EMVwzRCrq39C60jZXgjy4kzbeJ-4Eyj-gtsOqr8ibEs',
range: 'A1',
auth: authClient,
valueInputOption: 'RAW',
insertDataOption: 'INSERT_ROWS',
resource: {
values: [
[name,
email,
phone,
city,
college,
year,
gender,
...selectedSports
]
]
}
};
sheet.spreadsheets.values.append(request2, function (err, response) {
if (err)
console.log(err)
});
var request = {
spreadsheetId: '11sUzVRd1EBhqrcAUUbOHwCAvSzG2pbJaufM1bk2z6L8',
range: 'A1',
auth: authClient,
valueInputOption: 'RAW',
insertDataOption: 'INSERT_ROWS',
resource: {
values: [
[name,
email,
phone,
city,
college,
year,
gender,
...selectedSports
]
]
}
};
await sheet.spreadsheets.values.append(request, function (err, response) {
if (err) {
console.error(err);
res.status(500).send()
}
res.status(200).send()
});
});
})
async function authorize(callback) {
var authClient = new google.auth.JWT(keys.client_email, null, keys.private_key, [
'https://www.googleapis.com/auth/spreadsheets'
]);
if (authClient == null) {
console.log('authentication failed');
return;
}
callback(authClient);
}
app.listen(port, () => console.log(`listening on port ${port}`))