-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·454 lines (405 loc) · 13.7 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
This file is the backbone of mytorch app
Main setup using Express.js
*/
var express = require( 'express' )
, app = express()
, server = require( 'http' ).createServer( app )
, passport = require( 'passport' )
, util = require( 'util' )
, bodyParser = require( 'body-parser' )
, cookieParser = require( 'cookie-parser' )
, session = require( 'express-session' )
, RedisStore = require( 'connect-redis' )( session )
, GoogleStrategy = require( 'passport-google-oauth2' ).Strategy
, pug = require( 'pug')
, mongoose = require('mongoose')
//, favicon = require( 'serve-favicon')
, port = process.env.PORT || 8002;
https = require('https');
///////////////////////////////////////////////////////////////////////
//Express.js Configuration
///////////////////////////////////////////////////////////////////////
/*
all public files lives in the public directory
all html/front end pages are rendered using pug.js and live in the public/views folder
*/
app.use(express.static('public'));
app.set("view engine", "pug");
app.set("views", "public/views");
//app.use(favicon('public','favicon.ico'));
/*
Extra set up for bodyParser. Necessary for parsing request and response queries
*/
app.use( cookieParser());
app.use( bodyParser.json());
app.use( bodyParser.urlencoded({
extended: true
}));
app.listen(port, function() {
console.log('Server started on port ', port);
});
/*
Set up default mongoose connection
You can view the results of the database on heroku dashboard when deployed
Local database resets everytime though
*/
var mongoDB =
process.env.MONGOLAB_URI ||
process.env.MONGODB_URI ||
process.env.MONGOHQ_URL ||
'mongodb://localhost/my_database';
mongoose.connect(mongoDB, {useMongoClient: true}, function(err){
if (err) {
console.log ('ERROR connecting' + err);
} else {
console.log ('Succeeded connected');
}
});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
///////////////////////////////////////////////////////////////////////
//Google Passport Authentication Set-up
///////////////////////////////////////////////////////////////////////
var GOOGLE_CLIENT_ID = "940480382034-7tdll4daeqk1n5nja83e3cp4untm9dtf.apps.googleusercontent.com"
, GOOGLE_CLIENT_SECRET = "DANHTfljRXhm8zkPdiTGkO5Y";
// Passport session setup
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
// Use the GoogleStrategy within Passport.
passport.use(new GoogleStrategy({
clientID: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
callbackURL: "/auth/google/callback",
passReqToCallback : true
},
function(request, accessToken, refreshToken, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
return done(null, profile);
});
}
));
/*
saving session
*/
app.use(cookieParser());
app.enable('trust proxy'); // add this line
app.use( session({
secret: 'secret',
resave: true,
saveUninitialized: true,
proxy: true, // add this line
cookie: {
secure: false, //set to false because we are not on https
maxAge: 3600000
}
}));
app.use( passport.initialize());
app.use( passport.session());
/*
Accessing the main URL will first check if the user is authenticated and render the login page
*/
app.get('/', ensureAuthenticated, function(req, res){
res.render('login.pug');
});
/*
going directly to the login page
*/
app.get('/login', function(req, res){
res.render('login.pug');
});
/*
login redirect to the Google login page
*/
app.get('/auth/google', passport.authenticate('google', { successRedirect: '/',scope:
['email']
}));
/*
the redirect after successful login
*/
app.get('/auth/google/callback',function(req, res, next) {
passport.authenticate('google', function(err, user, info) {
if (err) { return next(err); }
if (!user) { return res.redirect('/login'); }
req.logIn(user, function(err) {
if (err) { return next(err); }
req.session.save(() => {
setTimeout(function(){
lookup_db(user.email, res)
}, 1000)
return;
})
});
})(req, res, next);
});
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
// Simple route middleware to ensure user is authenticated.
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
console.log('unauth')
res.redirect('/login');
}
///////////////////////////////////////////////////////////////////////
//Database interaction
///////////////////////////////////////////////////////////////////////
var DB = {}
function lookup_db(email, res){
//check if nyu email
if ((!email.endsWith("@nyu.edu"))){
console.log('not nyu', email);
return res.redirect('/login');
}
//check if first visit
usermodel.findOne({myemail: email}, function(err, found_user){
if (err) {
console.log("The error while accessing the colleciton is " + err);
}
if (found_user){
res.redirect('/home');
console.log('new session with returning user: ', found_user);
} else {
res.redirect('/tutorial');
}
})
}
/*
Database schema for storing the user's friends contact numbers and netIDs
*/
var Schema = mongoose.Schema;
var userschema = new Schema({
myemail: String,
myphone: String,
name1: String,
name2: String,
name3: String,
phone1: String,
phone2: String,
phone3: String,
netid1: String,
netid2: String,
netid3: String,
});
var usermodel = mongoose.model('usermodel', userschema );
function add_or_update(req){
usermodel.findOne({myemail: req.session.passport.user.email}, function(err, found_user){
if (err) {
console.log("The error while updating colleciton is " + err);
}
console.log(req.session)
console.log(req.body)
if (found_user){
console.log('Updating existing information:', found_user)
found_user.myphone = req.body.myphone
found_user.name1 = req.body.name2
found_user.name2 = req.body.name3
found_user.name3 = req.body.name3
found_user.phone1 = req.body.phone1
found_user.phone2 = req.body.phone2
found_user.phone3 = req.body.phone3
found_user.netid1 = req.body.netid1
found_user.netid2 = req.body.netid2
found_user.netid3 = req.body.netid3
found_user.save(function(err) {
if (err)
throw err
return (null, found_user)
});
}
if (!found_user) {
console.log('Saving new user', req.body, req.session.passport.user.email)
var newuser = new usermodel({
myemail: req.session.passport.user.email,
myphone: req.body.myphone,
name1: req.body.name2,
name2: req.body.name3,
name3: req.body.name3,
phone1: req.body.phone1,
phone2: req.body.phone2,
phone3: req.body.phone3,
netid1: req.body.netid1,
netid2: req.body.netid2,
netid3: req.body.netid3,
});
newuser.save(function (err, fluffy) {
if (err) return console.error(err);
});
}
})
}
var recordschema = new Schema({
email: String,
situ: String,
who: String,
what: String,
when: String
});
var recordmodel = mongoose.model('recordmodel', recordschema );
function add_record(req, sit){
//add the record to our database
console.log(req)
var dt = new Date();
var newrecord = new recordmodel({
email: req.session.passport.user.email,
situ: sit,
who: req.body.who,
what: req.body.what,
when: dt.toUTCString()
});
newrecord.save(function (err, fluffy) {
if (err) return console.error(err);
});
}
function print_db(){
// PRINTS ALL Entries
usermodel.find(function (err, kittens) {
if (err) return console.error(err);
console.log(kittens);
})
// recordmodel.find(function (err, kittens) {
// if (err) return console.error(err);
// console.log(kittens);
// })
}
///////////////////////////////////////////////////////////////////////
//Twillio Text Messaging API
///////////////////////////////////////////////////////////////////////
/*
the receipient number variable is currently hardcoded to go to Gabor Csapo's phone number
*/
var accountSid = 'AC0b4dd92564225048f717aaa016bab864';
var authToken = '36dd70b5af39ea604ed0e883a7a2df9d';
var client = require('twilio')(accountSid, authToken);
/*
below recipient variable should be deleted and the corresponding phone number
for each help request should be passed as a parameter to the send_text() function.
*/
var recipient = '+971563052997';
/*
This is the main send_text() method to send messages to appropriate bodies.
Right now it's receiving the query body from the GET request from home.pug
The locationStr is the string to indicate the address string
If the student has a phone number, it is sent in the SMS to allow direct contact
*/
function send_text(reqBody, recipient, user){
var locationStr = reqBody.building + reqBody.buildingRes + " " + reqBody.room
usermodel.findOne({myemail: user}, function(err, found_user){
if (err) {
console.log("The error while accessing the colleciton is " + err);
}
var messageBody = "EMERGENCY. Urgent help requested at " + locationStr + " for "+reqBody.situation+". Please go to the location now."
if (found_user){
messageBody = messageBody + ' Contact student at: ' + found_user.myphone
}
client.messages.create({
to: recipient,
from: "+16093725592",
body: messageBody,
}, function(err, messageres) {
if (err){
console.log(err)
}
console.log('SMS sent', messageres.sid);
});
})
}
///////////////////////////////////////////////////////////////////////
//Routes, MAKE SURE TO AUTHENTICATE
///////////////////////////////////////////////////////////////////////
app.get('/home', ensureAuthenticated, function(req, res, err) {
res.render('home.pug');
});
/*
Tutorial page is what the user sees when they register for the first time
This page prompts users to input their and their friends' contact info
*/
app.get('/tutorial', ensureAuthenticated, function(req, res, err) {
console.log(req)
res.render('tutorial.pug')
});
/*
Contacts page is where the user can edit their or their friends' contact info
*/
app.get('/contacts', ensureAuthenticated, function(req, res, err) {
res.render('contacts.pug')
});
/*
When the user clicks on save contacts, the database will be added or updated
The user will then be redirected to the main home page
*/
app.post('/contacts/saved', ensureAuthenticated, function(req, res, err) {
add_or_update(req)
print_db()
res.redirect('/home')
});
/*
This page is when you press for help from Public Safety
It renders the emergency.pug view and pass on the parameters such as the location and situation
It then sends text to the public safety number, which will be set within the router below
*/
app.get('/emergency', ensureAuthenticated, function(req, res, err) {
res.render('emergency.pug', {'building': req.query.building, 'room': req.query.room,
'situation': req.query.situation, 'buildingRes': req.query.buildingRes, 'helpFrom' : 'Public Safety'});
console.log(req.query)
//UNCOMMENT THIS AND INPUT PUBLIC SAFETY's NUMBER
//var recipient = PUBLIC SAFETY'S NUMBER
send_text(req.query, recipient, req.session.passport.user.email)
});
/*
This page is when you press for help from RA's on duty
It renders the emergency.pug view and pass on the parameters such as the location and situation
It then sends text to the RA's number, which will be set within the router below
*/
app.get('/danger', ensureAuthenticated, function(req, res, err) {
res.render('emergency.pug', {'building': req.query.building, 'room': req.query.room,
'situation': req.query.situation, 'buildingRes': req.query.buildingRes, 'helpFrom' : 'RAs on duty'});
//UNCOMMENT THIS AND INPUT RA's NUMBER
//var recipient = RA'S NUMBER
send_text(req.query, recipient, req.session.passport.user.email)
});
/*
This page is when you press for help from your friends
It renders the emergency.pug view and pass on the parameters such as the location and situation
It then sends texts to your friends' numbers, which will be retrieved and then set within the router below
*/
app.get('/friends', ensureAuthenticated, function(req, res, err) {
res.render('emergency.pug', {'building': req.query.building, 'room': req.query.room,
'situation': req.query.situation, 'buildingRes': req.query.buildingRes, 'helpFrom' : 'your friends'});
//Below should be a for loop to iterate through all of your friends' numbers
//var recipient = RA'S NUMBER
send_text(req.query, recipient, req.session.passport.user.email)
});
/*
The three routes below are for submitting help requests.
The user's report on the incident will be saved to the database
They then render the emergecy-submit.pug which contains a link to safety tips
*/
app.post('/emergency/submit', ensureAuthenticated, function(req, res, err) {
console.log(req.body);
res.render('emergency-submit.pug');
add_record(req, 'emergency');
});
app.post('/danger/submit', ensureAuthenticated, function(req, res, err) {
console.log(req.body);
res.render('emergency-submit.pug');
add_record(req, 'danger');
});
app.post('/friends/submit', ensureAuthenticated, function(req, res, err) {
console.log(req.body);
res.render('emergency-submit.pug');
add_record(req, 'sub-danger');
});
/*
This tips page is where first aid response tips live.
Currently the user is shown help texts for three scenarios
*/
app.get('/tips', ensureAuthenticated, function(req, res, err) {
res.render('tips.pug')
});