-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
316 lines (264 loc) · 9.25 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const MongoClient = require('mongodb').MongoClient;
const jwt = require('jsonwebtoken');
const account = require('./accounts');
const mail = require('./email');
const path = require('path');
const id = require('mongodb').ObjectId;
const password = 'shhhhh';
const base_url = 'https://cop4331-g25.herokuapp.com/';
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.set('port', (process.env.PORT || 5000));
app.use(express.static(path.join(__dirname, 'frontend', 'build')));
app.get('/cards', (req, res) =>
{
res.sendFile(path.join(__dirname, 'frontend', 'build', 'index.html'))
});
app.post('/api/jwtTest', async(req, res, next) =>{
// const {userName, Password} = req.body;
// account.login(res, 'Users', userName, Password);
// var token = jwt.sign(req.body, 'shhhhh');
var decoded = jwt.verify(req.body.token, password);
res.status(200).json(decoded);
});
app.post('/api/jwtSign', async(req, res, next) =>{
var decoded = jwt.sign(req.body, password);
res.status(200).json({token:decoded});
});
// The fallowing functions work and can be uncommented if you want to use them
// however they are unsafe, i use them for testing. If you need the functionality
// form one of them, let me know and ill make a more user friendly version for you
// thanks!
// ------------------------- unsafe low level functions --------------------------- //
// app.post('/api/add', async (req, res, next) => {
// const { table, filter } = req.body;
// var results = await account.db.add(table, filter);
// account.db.sendjson(res, results);
// });
// app.post('/api/delete', async (req, res, next) => {
// const { table, filter } = req.body;
// var results = account.db.delete(table, filter);
// account.db.sendjson(res, results);
// });
// app.post('/api/update', async (req, res, next) => {
// const { table, filter1, filter2 } = req.body;
// var results = account.db.update(table, filter1, filter2);
// account.db.sendjson(res, results);
// });
// app.post('/api/searchExact', async (req, res, next) => {
// const { table, filter } = req.body;
// var results = await account.db.find(table, filter);
// account.db.sendjson(res, results);
// });
// ----------------------- end of unsafe low level functions ---------------------- //
// Before each of these functions is an example json input describing what info
// the function expects, it can be coyied and pasted for immediate use
// ------------------------- safe high level functions --------------------------- //
// {
// "userName": "543212",
// "Password": "12345"
// }
app.post('/api/loginUser', async(req, res, next) =>{
var decoded = jwt.verify(req.body.token, password);
const {userName, Password} = decoded;
account.login(res, 'Users', userName, Password);
});
// {
// "userName": "543212",
// "Password": "12345"
// }
app.post('/api/loginAdmin', async(req, res, next) =>{
var decoded = jwt.verify(req.body.token, password);
const {userName, Password} = decoded;
account.login(res, 'Admins', userName, Password);
});
// {
// "userName": "54321",
// "Password" : "12345",
// "firstName" : "Users",
// "lastName" : "12345",
// "email": "543212",
// "admin" : 5
// }
app.post('/api/registerUser', async(req, res, next) =>{
var decoded = jwt.verify(req.body.token, password);
const {userName, Password, firstName, lastName, email, admin} = decoded;
account.register(res, 'Users', userName, Password, firstName, lastName, email, admin);
});
// {
// "userName": "54321",
// "Password" : "12345",
// "firstName" : "Users",
// "lastName" : "12345",
// "email": "543212"
// }
app.post('/api/registerAdmin', async(req, res, next) =>{
var decoded = jwt.verify(req.body.token, password);
const {userName, Password, firstName, lastName, email} = decoded;
account.register(res, 'Admins', userName, Password, firstName, lastName, email);
});
// {
// "userName": "543212",
// "startLocation": "",
// "destination": "",
// "purpose": "",
// "weather": "",
// "startTime": ""
// }
app.post('/api/makeTrip', async(req, res, next) =>{
var decoded = jwt.verify(req.body.token, password);
const {userName, startLocation, destination, purpose, weather, startTime} = decoded;
account.makeTrip(res, userName, startLocation, destination, purpose, weather, startTime);
});
// {
// "tripId": "5efe59a2f1211c174a11d441",
// "isApproved": true,
// "comments": "sounds good"
// }
app.post('/api/approveTrip', async(req, res, next) =>{
var decoded = jwt.verify(req.body.token, password);
const {tripId, isApproved, comments} = decoded;
account.approveTrip(res, tripId, isApproved, comments);
});
// NOTE: please dont use my email for testing, it does work but you wont see if the email
// went through or not, and I will be confused about the extra emails in my inbox
// {
// "to" : "[email protected]",
// "subject": "543212",
// "text": "test email",
// "html": ""
// }
app.post('/api/sendMail', async(req, res, next) =>{
var decoded = jwt.verify(req.body.token, password);
const {to, subject, text, html} = decoded;
mail.send(res, to, subject, text, html);
});
// no input required
app.post('/api/listAdmins', async(req, res, next) =>{
account.listAdmins(res);
});
// this endpoint does not require a normal input,
// instead the url is the input. after the normal url
// you add "?userName=<user>&Password=<pass>" so the url would look something like
// https://www.largeproject.app/api/Verify?userName=Michael12345&Password=securePass
// in that way the user can simply click the link in there email
// to verify there account.
// it can also be called form an html button embedded in an email
app.get('/api/Verify', async(req, res, next) =>{
// var userName = require('url').parse(req.url,true).query.userName;
// var Password = require('url').parse(req.url,true).query.Password;
var Id = require('url').parse(req.url,true).query.Id;
account.verify(res, Id);
});
// {
// "userName":"user1",
// "Password":"pass"
// }
app.post('/api/sendResetPassword', async(req, res, next) =>{
// var userKey = require('url').parse(req.url,true).query.Key;
var decoded = jwt.verify(req.body.token, password);
var {userName, Password} = decoded;
var user = await account.db.find('Users', {userName:userName});
var admin = await account.db.find('Admins', {userName:userName});
var id;
var to;
if(user.Results.length > 0)
{
id = user.Results[0]._id;
to = user.Results[0].email;
}
else
{
id = admin.Results[0]._id;
to = admin.Results[0].email;
}
var key = jwt.sign({Password:Password}, password);
var text = "Password reset link:\n" + base_url + 'api/Reset?Id=' + id + '&Key=' + key;
mail.send(res, to, "Password Reset", text, "");
account.db.sendjson(res, {error:""});
});
app.get('/api/Reset', async(req, res, next) =>{
// var userName = require('url').parse(req.url,true).query.userName;
// var Password = require('url').parse(req.url,true).query.Password;
var Id = require('url').parse(req.url,true).query.Id;
var key = require('url').parse(req.url,true).query.Key;
var Key = jwt.verify(key, password).Password;
var login = {_id: id.ObjectId(Id)};
var change = {Password: Key};
var user = await account.db.update("Users", login, change);
var admin = await account.db.update("Admins", login, change);
// account.db.sendjson(res, {Results: 'Password Changed!',error:''});
res.redirect('https://cop4331-g25.herokuapp.com');
});
// {
// "userName": "myusername"
// }
app.post('/api/listTripsByUser', async(req, res, next) =>{
var decoded = jwt.verify(req.body.token, password);
var {userName} = decoded;
account.listTripsByUser(res, userName);
});
// {
// "userName": "myusername"
// }
app.post('/api/listTripsByAdmin', async(req, res, next) =>{
var decoded = jwt.verify(req.body.token, password);
var {userName} = decoded;
account.listTripsByAdmin(res, userName);
});
// the fields in new data do not all need to be included, only the ones you want to change
// {
// "_id":"5efe59a2f1211c174a11d441",
// "newData":
// {
// "userId":"5efe51c3d435ff137d3977c3",
// "adminId":"5efe59a2f1211c174a11d441",
// "startLocation":"",
// "destination":"",
// "locations":[],
// "progress":0,
// "startTime":"",
// "endTime":"",
// "purpose":"",
// "isApproved":true,
// "isNew":false,
// "comments":"sounds good",
// "weather":"",
// }
// }
app.post('/api/editTrip', async(req, res, next) =>{
var decoded = jwt.verify(req.body.token, password);
var {_id, newData} = decoded;
account.editTrip(res, _id, newData);
});
// {
// "Id":"5efe51c3d435ff137d3977c3"
// }
app.post('/api/getById', async(req, res, next) =>{
var decoded = jwt.verify(req.body.token, password);
var {Id} = decoded;
account.getById(res, Id);
});
app.post('/api/test', async(req, res, next) =>{
console.log(6);
});
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept, Authorization'
);
res.setHeader(
'Access-Control-Allow-Methods',
'GET, POST, PATCH, DELETE, OPTIONS'
);
next();
});
var port = process.env.PORT || 5000;
app.listen(port, () => {
console.log("Server running on port " + port);
}); // start Node + Express server on port 5000