-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
678 lines (592 loc) Β· 17.6 KB
/
app.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
//TODO: LOAD IN ALL CONFESSIONS USING FACEBOOK API RATHER THAN LINKING TO THE FB PAGE!
//req.signedCookies['session']
// USE CONFIG FOR API KEYS
// process.env.
//Survey API -> http://surveybths.com/wp-json/wp/v2/posts/23
const cookieParser = require('cookie-parser');
const cookieEncrypter = require('cookie-encrypter')
const algoliasearch = require('algoliasearch');
const dotenv = require('dotenv');
dotenv.config();
const algolia = algoliasearch(
process.env.ALGOLIA_APP_ID,
process.env.ALGOLIA_API_KEY
);
const index = algolia.initIndex(process.env.ALGOLIA_INDEX_NAME);
var express = require('express')
var app = express()
app.listen(process.env.PORT || 8080);
let success = "Success!"
var bodyParser = require("body-parser");
app.use(express.static(__dirname + '/public'))
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(express.static(__dirname + '/public'))
app.set('view engine', 'jade');
//Firebase operations
var firebase = require('firebase')
var admin = require('firebase-admin');
var serviceAccount = require("./thefffoundry-firebase-adminsdk-dy2sw-9622fa9200.json");
var config = {
apiKey: process.env.FIREBASE_API_KEY,
authDomain: "thefffoundry.firebaseapp.com",
databaseURL: "https://thefffoundry.firebaseio.com",
projectId: "thefffoundry",
storageBucket: "",
messagingSenderId: process.env.FIREBASE_SENDER_ID,
appId: process.env.FIREBASE_APP_ID
};
firebase.initializeApp(config);
app.use(cookieParser(process.env.COOKIE_SECRET));
app.use(cookieEncrypter(process.env.COOKIE_SECRET));
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://thefffoundry.firebaseio.com"
});
const db = firebase.database();
const ref = db.ref();
const moment = require('moment')
const clubss = ref.child("clubs");
const freshmen = ref.child("freshmenPosts");
const sophomores = ref.child("sophomorePosts");
const juniors = ref.child("juniorPosts");
const seniors = ref.child("seniorPosts");
const suggestionss = ref.child("suggestions");
//Search functions
const contactsRef = db.ref('/clubs');
contactsRef.on('child_added', addOrUpdateIndexRecord);
contactsRef.on('child_changed', addOrUpdateIndexRecord);
contactsRef.on('child_removed', deleteIndexRecord);
function addOrUpdateIndexRecord(contact) {
// Get Firebase object
const record = contact.val();
// Specify Algolia's objectID using the Firebase object key
record.objectID = contact.key;
// Add or update object
index
.saveObject(record)
.then(() => {
console.log('Firebase object indexed in Algolia', record.objectID);
})
.catch(error => {
console.error('Error when indexing contact into Algolia', error);
process.exit(1);
});
}
function deleteIndexRecord({key}) {
// Get Algolia's objectID from the Firebase object key
const objectID = key;
// Remove the object from Algolia
index
.deleteObject(objectID)
.then(() => {
console.log('Firebase object deleted from Algolia', objectID);
})
.catch(error => {
console.error('Error when deleting contact from Algolia', error);
process.exit(1);
});
}
//end
//nodemailer start
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: process.env.FOUNDRY_EMAIL_PASS
}
});
var mailOptions = {
};
//end
function addSuggestion(name, osis, description, type){
suggestionss.push({
name: name,
osis: osis,
description: description,
type: type,
})
}
function addClub(name, description, drive, calendar, creator, unique, type){
clubss.push({
name: name,
description: description,
drive: drive,
calendar: calendar,
creator: creator,
time: moment().format(),
uniquedesc: unique,
type: type,
})
}
function addFreshmen(content, author) {
freshmen.push({
post: content,
time: moment().format(),
author: author,
});
}
function addSophomores(content, author) {
sophomores.push({
post: content,
time: moment().format(),
author: author,
});
}
function addJuniors(content, author) {
juniors.push({
post: content,
time: moment().format(),
author: author,
});
}
function addSeniors(content, author) {
seniors.push({
post: content,
time: moment().format(),
author: author,
});
}
//check if admin
async function checkRole(email) {
let status = false;
await admin.auth().getUserByEmail(email).then((userRecord) => {
// The claims can be accessed on the user record.
console.log(email + ': ' + userRecord.customClaims.admin);
if (userRecord.customClaims.admin == true) {
status = true;
} else {
status = false;
}
});
console.log(status)
return status;
}
//setting custom claims for admins
async function addMod(email, emailCurrentUser) {
const user = await admin.auth().getUserByEmail(email);
const uid = user.uid;
checkRole(emailCurrentUser).then(check => {
if (check == true) {
console.log('Authorized!')
console.log(uid);
admin.auth().setCustomUserClaims(uid, {
admin: true
}).then(() => {});
} else {
console.log('Request unauthorized.')
}
});
};
async function removeMod(email, emailCurrentUser) {
const user = await admin.auth().getUserByEmail(email);
const uid = user.uid;
checkRole(emailCurrentUser).then(check => {
if (check == true) {
console.log('Authorized!')
console.log(uid);
admin.auth().setCustomUserClaims(uid, {
admin: false
}).then(() => {});
} else {
console.log('Request unauthorized.')
}
});
};
//End of firebase operations
app.get('/', function(req, res) {
res.render('index')
})
app.post('/postSignUp', function(req, res) {
console.log(req.body);
let password = req.body.password;
let email = req.body.email;
firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorMessage);
res.render('login', {
error: errorMessage
})
}).then(
).catch(
// cannot check cookies as they do not exist yet.
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
success = "You've successfully registered!";
res.cookie('session', firebase.auth().currentUser, { signed: true, maxAge: 900000, httpOnly: true });
res.set('cache-control', 'max-age=0, private') // may not be needed. Good to have if behind a CDN.
removeMod(firebase.auth().currentUser.email.replace(/^"(.*)"$/, '$1'),'[email protected]')
res.render('login', {
success: success
})
return firebase.auth().signOut(); //clears session from memory
}
})
)
})
app.get('/login', function(req, res) {
res.render('login')
})
app.get('/logout', function(req, res) {
let user = req.signedCookies['session'];
console.log(user)
if (user) {
res.clearCookie("session")
res.render('login', {
success: 'Logged out!'
})
} else {
res.render('login', {
error: 'Log in first.'
})
}
});
app.post('/postSignIn', function(req, res) {
console.log(req.body);
let password = req.body.password;
let email = req.body.email;
let user = req.signedCookies['session'];
if (user) {
success = "You're already logged in!";
res.render('login', {
success: success
})
} else {
firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorMessage)
res.render('login', {
error: errorMessage
})
})
.then(
).catch(
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
success = "You're logged in!";
res.cookie('session', firebase.auth().currentUser, { signed: true, maxAge: 900000, httpOnly: true });
res.set('cache-control', 'max-age=0, private') // may not be needed. Good to have if behind a CDN.
res.render('login', {
success: success
})
return firebase.auth().signOut(); //clears session from memory
} else {
res.render('login', {
error: errorMessage
})
}
})
);
}
})
app.get('/admin', function(req, res) {
if (req.signedCookies['session']) {
checkRole(req.signedCookies['session'].email.replace(/^"(.*)"$/, '$1')).then(check => {
if (check == true) {
email = req.signedCookies['session'].email.replace(/^"(.*)"$/, '$1');
res.render('admindash', {
email: email,
suggestions: suggestions,
})
suggestions = [];
getData(suggestions, "suggestions")
} else {
res.render('login', {
error: 'Request unauthorized.'
})
}
})
} else {
res.render('login', {
error: 'Log in first.'
})
}
})
//TODO: Catch errors and successes
app.post('/addAdmin', function(req, res) {
let addEm = req.body.addEm;
addMod(addEm, req.signedCookies['session'].email.replace(/^"(.*)"$/, '$1')).catch(function(error) {
res.render('admindash', {
error: 'Did not work. User may not exist or permission may not be valid.'
});
}).then(data => {
res.render('admindash', {
success: 'User added!'
})
})
.catch(error => {
res.render('admindash', {
success: 'User added!'
})
});
})
app.post('/removeAdmin', function(req, res) {
let removeEm = req.body.removeEm;
removeMod(removeEm, req.signedCookies['session'].email.replace(/^"(.*)"$/, '$1')).catch(function(error) {
res.render('admindash', {
error: 'Did not work. User may not exist or permission may not be valid.'
});
}).then(data => {
res.render('admindash', {
success: 'User removed!'
})
})
.catch(error => {
res.render('admindash', {
success: 'User removed!'
})
});
})
// blog shenanigans
// TODO: Add support for author
// TODO: Add security things
app.post('/freshmen', function(req, res) {
console.log(req.body);
let content = req.body.content;
addFreshmen(content,req.signedCookies['session'].email.replace(/^"(.*)"$/, '$1'))
res.redirect('/admin');
})
app.post('/sophomores', function(req, res) {
console.log(req.body);
let content1 = req.body.content1;
addSophomores(content1,req.signedCookies['session'].email.replace(/^"(.*)"$/, '$1'))
res.redirect('/admin');
})
app.post('/juniors', function(req, res) {
console.log(req.body);
let content2 = req.body.content2;
addJuniors(content2,req.signedCookies['session'].email.replace(/^"(.*)"$/, '$1'))
res.redirect('/admin');
})
app.post('/seniors', function(req, res) {
console.log(req.body);
let content3 = req.body.content3;
addSeniors(content3,req.signedCookies['session'].email.replace(/^"(.*)"$/, '$1'))
res.redirect('/admin');
})
app.post('/postSuggestion', (req, res) => {
console.log(req.body);
let name = req.body.name.trim();
let osis = req.body.osis.trim();
let description = req.body.desc.trim();
let type = req.body.type;
if (req.signedCookies['session']) {
addSuggestion(name, osis, description, type)
res.render("suggestions", {success: "Suggestion sent!"})
} else {
res.render('suggestions', {
error: 'Log in first.'
})
}
});
//get
//TODO: Finish!!!
//TODO: Load in posts!
//test
// ref.on("value", function(snapshot) {
// const post = snapshot.val();
// console.log(post.child("freshmPosts/ada"));
// console.log(post.sophomorePosts);
// console.log(post.juniorPosts);
// console.log(post.seniorPosts);
//
// }, function (errorObject) {
// console.log("The read failed: " + errorObject.code);
// });
let freshmenPostss = [];
let sophomorePostss = [];
let juniorPostss = [];
let seniorPostss = [];
let clubs = [];
let clubName = [];
let suggestions = [];
async function getData(data, ref) {
var query = firebase.database().ref(ref).orderByKey();
query.once("value")
.then(function(snapshot) {
// ref.on("child_added", function(snapshot, prevChildKey) {
// var newPost = snapshot.val();
// console.log("Author: " + newPost.author);
// });
snapshot.forEach(function(childSnapshot) {
// key will be "ada" the first time and "alan" the second time
var key = childSnapshot.key;
// childData will be the actual contents of the child
var childData = childSnapshot.val();
// console.log(childData)
data.push(childData)
});
})
}
getData(clubs, "clubs")
getData(freshmenPostss, "freshmenPosts")
getData(sophomorePostss, "sophomorePosts")
getData(juniorPostss, "juniorPosts")
getData(seniorPostss, "seniorPosts")
getData(suggestions, "suggestions")
//end test
//TODO: FINISH
app.get('/freshmenget', function(req, res) {
res.render('freshmenget', {
freshmenPosts: freshmenPostss
})
freshmenPostss = [];
getData(freshmenPostss, "freshmenPosts")
});
app.get('/sophomoresget', function(req, res) {
res.render('sophomoresget', {
sophomorePosts: sophomorePostss
})
sophomorePostss = [];
getData(sophomorePostss, "sophomorePosts")
});
app.get('/juniorsget', function(req, res) {
res.render('juniorsget', {
juniorPosts: juniorPostss
});
juniorPostss = [];
getData(juniorPostss, "juniorPosts")
})
app.get('/seniorsget', function(req, res) {
res.render('seniorsget', {
seniorPosts: seniorPostss
});
seniorPostss = [];
getData(seniorPostss, "seniorPosts")
})
app.get('/userdash', function(req, res) {
if (req.signedCookies['session']) {
res.render('userdash', {
user: req.signedCookies['session'].email.replace(/^"(.*)"$/, '$1')
})
} else {
res.render('login', {
error: 'Log in first.'
})
}
});
// add club approval feature
app.get('/clubs', function(req, res) {
res.render('clubs', {
clubs: clubs,
})
clubs = [];
getData(clubs, "clubs")
});
async function checkClubSend(array, name, req){
console.log('starting')
array.forEach(function(club) {
if(club.name == name){
mailOptions = {
from: '[email protected]',
to: req.signedCookies['session'].email.replace(/^"(.*)"$/, '$1'),
subject: `${req.signedCookies['session'].email.replace(/^"(.*)"$/, '$1')} wants to join ${name}!`,
text: `Hey ${club.creator}! A student wants to join ${name}! Email them (${req.signedCookies['session'].email.replace(/^"(.*)"$/, '$1')}) to accept or reject their request!`
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
}
})
}
app.get('/join/:position', (req, res) => {
let position = req.params.position;
clubNameJoin = clubs.map(({ name }) => name)
if (clubNameJoin.includes(position)) {
if (req.signedCookies['session']) {
checkClubSend(clubs, position, req);
res.render('clubdash', {clubName: position, allClubs: clubNameJoin, clubs: clubs, success: 'Request sent by email!'})
}
else{
res.render('clubdash', {clubName: position, allClubs: clubNameJoin, clubs: clubs, error: 'Log in first.'})
}
}
else if (clubNameJoin.includes(position)==false){
res.redirect('/404')
console.log("bork")
}
clubNameJoin = [];
});
app.get("/clubs/:position", (req, res) => {
let position = req.params.position;
clubName = clubs.map(({ name }) => name)
if (clubName.includes(position)) {
res.render('clubdash', {clubName: position, allClubs: clubName, clubs: clubs});
//res.send(`<h1>${position} exists, but we're working on this feature. Please email the president to join at this moment. Updates are coming soon!</h1><br><h2>All clubs: ${clubName}</h2><br><h2>VERSION 0.1.2</h2>`);
} else if (clubName.includes(position)==false){
res.redirect('/404')
}
clubName = [];
});
app.get('/suggestions', (req, res) => {
res.render('suggestions', {})
});
app.get('/makeClub', function(req, res) {
console.log("cookie: " + req.signedCookies['session'])
if (req.signedCookies['session']) {
res.render('makeClub')
} else {
res.render('clubs', {
error: 'Log in first.',
clubs: clubs,
})
}
});
app.get('/vote', function(req, res) {
res.render('polls')
})
// testing different styles
app.get('/privacy', function(req, res) {
res.render('testprivacy')
})
app.get('/about', function(req, res) {
res.render('testabout')
})
// end test
//maybe this should be an admin only feature?
app.get('/makePoll', function(req, res) {
if (req.signedCookies['session']) {
res.send('<h1>Feature is currently incomplete! Updates are coming soon!</h1><br><h2>VERSION 0.1.2</h2>')
} else {
res.render('polls', {
error: 'Log in first.'
})
}
});
app.post('/postClub', (req, res) => {
console.log(req.body)
let unique = req.body.unique.trim();
let type = req.body.type;
let name = req.body.club_name.trim();
let description = req.body.desc.trim();
let drive = req.body.drive.trim();
let calendar = req.body.calendar.trim();
let creator = req.signedCookies['session'].email.replace(/^"(.*)"$/, '$1');
console.log("creator" + JSON.stringify(creator))
addClub(name, description, drive, calendar, creator, unique, type)
//TODO: do some function to add club to database
res.render('clubs', {
success: "Club made! Click on clubs tab to see changes.",
clubs: clubs,
})
clubs = [];
getData(clubs, "clubs")
});
app.get('/*', function(req, res) {
res.render('404')
})
app.get('/404', function(req, res) {
res.render('404')
})
app.listen(8000, () => {
console.log('*NOS ORTUM SUPRA*');
});