-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
791 lines (734 loc) · 25.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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
//jshint esversion:6
const express = require('express');
const bodyParser = require('body-parser');
const ejs = require('ejs');
const md5 = require('md5');
const session = require('express-session');
const passport = require('passport');
const LocalStrategy = require("passport-local").Strategy;
const fs = require("fs");
const app = express();
app.use(express.static("public"));
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(session({
secret: 'Hellopc12',
resave: false,
saveUninitialized: false,
//cookie: { secure: true }
}));
app.use(passport.initialize());
app.use(passport.session());
const mysql = require('mysql');
var con = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'Twitter'
});
con.connect(function (err) {
if (err) console.log(err);
console.log("Connected!");
});
app.use((req, res, next) => {
res.locals.isAuthenticated = req.isAuthenticated();
next();
});
//-------- use the LocalStrategy
passport.use(new LocalStrategy(
//username and password are the name
//of the fields in the login form
function (username, password, done) {
//check if data is being passed
console.log(`
----------USERNAME: ${username},
----------PASSWORD: ${password}
`);
//make sure the user exists on the database
let $checkUser = "SELECT EMAIL, PASSWORD from REGISTRATION WHERE EMAIL = ?";
let $username = username;
let $queryUser = con.query($checkUser, [$username], (err, result) => {
if (err) {
console.log(err);
//passport error manager
done(err);
}
// if theres no data or result returns as 0 cancel auth
if (result == null || result == undefined || result.length === 0) {
console.log("USER NOT FOUND");
//set auth to false
done(null, false);
} else {
// else the user is in the database
// now we need to check if the passwords match
// matching the hashed passwords btw
let $password = md5(password); // this will be hashed automatically by bcrypt compare function
let $hash = result[0].PASSWORD;
//if theyre match auth the user
console.log("hash and password", $hash, $password, result[0].EMAIL);
if ($password === $hash) {
console.log("USER FOUND");
// this property user_id is created for passport to have it
// then we can access it using the req.user.user_id
// from the user object created by passport
return done(null, { user_id: result[0].EMAIL });
} else {
//else cancel auth
return done(null, false);
}
}
});
}
));
passport.serializeUser(function (user_id, done) {
done(null, user_id);
});
passport.deserializeUser(function (user_id, done) {
done(null, user_id);
});
function authMiddleware() {
return (req, res, next) => {
if (req.isAuthenticated()) {
return next();
} else {
res.redirect("/login");
}
}
}
app.get('/SignUp', function (req, res) {
if (req.isAuthenticated())
res.redirect('/');
else
res.render('makeProfile');
});
app.get('/', function (req, res) {
if (req.isAuthenticated()) {
if (req.user.user_id) {
$userId = req.user.user_id
} else {
//else look for the id in the req.session.passport.user
$userId = req.session.passport.user
}
console.log("making feed of ", $userId);
var sql = "SELECT TWEETS.TWEETID, TWEETS.userName as Username2,TWEETCONTENTS.userName, TweetContent FROM TWEETCONTENTS, FOLLOW,TWEETS WHERE follower = ? and following = TWEETS.userName AND TWEETS.TWEETID = TWEETCONTENTS.TWEETID order by TIMESTAMP_R desc";
var values = [
[$userId]
];
con.query(sql, [values], function (err, result) {
if (err) {
console.log(err);
res.redirect("/login");
}
else {
console.log(result);
// console.log("result[0]"+result[0]);
// if theres no data or result returns as 0 cancel auth4
var sql = "SELECT USERNAME, NAME, GENDER, BIO FROM USERS WHERE userName = ?";
var values = [
[$userId]
];
// console.log(result);
con.query(sql, [values], function (err, result2) {
if (err) {
console.log(err);
res.redirect("/login");
}
else {
console.log("result2" + result2);
res.render("home2", {
userId: result2[0].USERNAME,
bio: result2[0].BIO,
Gender: result2[0].GENDER,
tweets: result
});
}
});
// res.redirect('/');
}
});
}
else
res.redirect('/login');
});
app.post('/addUser', function (req, res) {
if (req.isAuthenticated())
return res.redirect('login');
const name = req.body.Name;
const bio = req.body.designation;
const email = req.body.email;
const Gender = req.body.Gender;
const userName = req.body.userName;
const password = md5(req.body.password);
console.log(name, bio, email, Gender, userName, password);
if (Gender === 'F' || Gender === 'f')
;
else if (Gender === 'M' || Gender === 'm')
;
else
return res.redirect('/signup');
var sql = 'SELECT * from REGISTRATION WHERE USERNAME = ? or EMAIL = ?';
var values = [
userName,
email
];
console.log("query=" + sql);
con.query(sql, values, function (err, result) {
if (err) {
console.log(err);
res.redirect("/signup");
}
else {
if (result === null || result === undefined || result.length == 0) {
var sql = "INSERT INTO USERS VALUES ?";
var values = [
[userName, name, Gender, bio]
];
con.query(sql, [values], function (err, result) {
if (err) {
console.log(err);
res.redirect("/signup");
}
else {
console.log("Number of records inserted: " + result.affectedRows);
var user_id = userName;
console.log(user_id);
var sql = "INSERT INTO REGISTRATION (EMAIL, USERNAME, PASSWORD) VALUES ?";
var values = [
[email, userName, password]
];
con.query(sql, [values], function (err, result) {
if (err) {
console.log(err);
res.redirect("/signup");
}
console.log(user_id, "Cookie");
req.login(user_id, () => {
res.redirect("/");
});
});
}
});
}
else
res.redirect("/signup");
}
});
});
app.get('/login', function (req, res) {
if (req.isAuthenticated())
res.redirect('/');
else res.render("login");
});
app.post('/login', function (req, res) {
const email = req.body.email;
const password = md5(req.body.password);
console.log(email, password);
var sql = "SELECT * FROM REGISTRATION WHERE EMAIL = ?";
var values = [
[email]
];
con.query(sql, [values], function (err, result) {
if (err) {
console.log(err);
res.redirect("/login");
}
else {
// if theres no data or result returns as 0 cancel auth
if (result == null || result == undefined || result.length === 0) {
console.log("USER NOT FOUND");
//set auth to false
res.redirect("/login");
} else {
let $password = password; // this will be hashed automatically by bcrypt compare function
let $hash = result[0].PASSWORD;
if ($password === $hash) {
console.log("USER FOUND");
var user_id = result[0].USERNAME;
console.log(user_id, "Cookie");
req.login(user_id, () => {
res.redirect("/");
})
} else
//else cancel auth
res.redirect("/login");
}
}
});
});
app.get('/people/', function name(req, res) {
if (req.isAuthenticated()) {
if (req.user.user_id) {
$userId = req.user.user_id
} else {
//else look for the id in the req.session.passport.user
$userId = req.session.passport.user
}
var sql = "select * from USERS where USERNAME <> ? and USERNAME not in (select FOLLOWING from FOLLOW where FOLLOWER = ?);";
var values = [
[$userId],
[$userId]
];
con.query(sql, values, function (err, result) {
if (err) {
console.log(err);
res.redirect("/login");
}
else {
// if theres no data or result returns as 0 cancel auth
// console.log(result);
res.render("people2", {
people: result
});
}
});
}
else
res.redirect('/login');
});
app.get('/people/:personId', function name(req, res) {
if (!req.isAuthenticated())
return res.redirect('/login');
console.log("Debug Startr");
$userId = req.params.personId;
console.log("making feed of ", $userId);
var sql = "SELECT TWEETS.TWEETID ,TWEETS.USERNAME as Username2, TWEETCONTENTS.USERNAME as username, TweetContent FROM TWEETCONTENTS,TWEETS WHERE TWEETS.TWEETID = TWEETCONTENTS.TWEETID AND TWEETS.USERNAME = ? order by TIMESTAMP_R desc";
var values = [
[$userId]
];
con.query(sql, values, function (err, result) {
if (err) {
console.log(err);
res.redirect("/");
}
else {
console.log(result);
// if theres no data or result returns as 0 cancel auth
if (result === "" || result === null)
res.redirect("/");
else {
// console.log(result);
var sql = "SELECT * FROM USERS WHERE userName = ?";
var values = [
[$userId]
];
con.query(sql, [values], function (err, result2) {
if (err) {
console.log(err);
console.log("+++++++++++++++++++++++++");
res.redirect("/");
}
else {
// if theres no data or result returns as 0 cancel auth
console.log("REs2" + result2);
if (result2 === "" || result2 === null || result2.length === 0)
res.redirect("/");
else {
if (req.user.user_id) {
$userId1 = req.user.user_id
} else {
//else look for the id in the req.session.passport.user
$userId1 = req.session.passport.user
}
if ($userId == $userId1) {
console.log(result2[0].NAME, result2[0].BIO);
res.render("person3", {
USERID: $userId,
FNAME: result2[0].NAME,
BIO: result2[0].BIO,
GENDER: result2[0].GENDER,
UNAME: $userId,
tweets: result,
choice: 0
});
}
else {
var sql = "SELECT * FROM FOLLOW WHERE FOLLOWER = ? and FOLLOWING = ?";
var value = [
[$userId1],
[$userId]
];
console.log($userId1, $userId);
con.query(sql, value, function (err, result3) {
if (err) {
console.log("||||||||||||||||||||||||");
console.log(err);
res.redirect('/');
}
else {
console.log("THIS MUST BE PRINTED");
console.log(result3);
if (result3.length === 0) {
res.render("person3", {
USERID: $userId,
FNAME: result2[0].NAME,
BIO: result2[0].BIO,
GENDER: result2[0].GENDER,
UNAME: $userId,
tweets: result,
choice: 1
});
}
else {
res.render("person3", {
USERID: $userId,
FNAME: result2[0].NAME,
BIO: result2[0].BIO,
GENDER: result2[0].GENDER,
UNAME: $userId,
tweets: result,
choice: 2
});
}
}
});
}
}
}
});
}
}
});
console.log("Here");
});
app.post('/searchPerson', function (req, res) {
res.redirect('/people/' + req.body.userName);
});
app.get('/follow/:personId', function (req, res) {
if (!req.isAuthenticated())
return res.redirect('login');
if (req.user.user_id) {
$userId = req.user.user_id
} else {
//else look for the id in the req.session.passport.user
$userId = req.session.passport.user
}
var follwingid = req.params.personId;
if (follwingid === $userId)
return res.redirect('/');
console.log("making feed of ", $userId);
var sql = "INSERT INTO FOLLOW (follower, following) VALUES ?";
var values = [
[$userId, follwingid]
];
con.query(sql, [values], function (err, result) {
if (err) {
console.log(err);
res.redirect("/");
}
else {
// if theres no data or result returns as 0 cancel auth
console.log(result);
res.redirect("/");
}
});
});
app.get('/unfollow/:personId', function (req, res) {
if (!req.isAuthenticated())
return res.redirect('login');
if (req.user.user_id) {
$userId = req.user.user_id
} else {
//else look for the id in the req.session.passport.user
$userId = req.session.passport.user
}
var follwingid = req.params.personId;
if (follwingid === $userId)
return res.redirect('/');
console.log("making feed of ", $userId);
var sql = "DELETE FROM FOLLOW where follower = ? and following = ?";
var values = [
[$userId],
[follwingid]
];
con.query(sql, values, function (err, result) {
if (err) {
console.log(err);
res.redirect("/");
}
else {
// if theres no data or result returns as 0 cancel auth
console.log(result);
res.redirect("/");
}
});
});
//adding starts
app.get('/deleteTweet/:personId/:orgusr/:TweetID', function (req, res) {
if (!req.isAuthenticated())
return res.redirect('login');
if (req.user.user_id) {
$userId = req.user.user_id
} else {
//else look for the id in the req.session.passport.user
$userId = req.session.passport.user
}
/*
if $user id != personid
redirect home
if personid == orgid
tweet content delete
else
tweets me se userid,tweetid
*/
var currid = req.params.personId;
var orgid = req.params.orgusr;
var tweetid = req.params.TweetID;
console.log(currid, orgid, tweetid);
// return res.redirect('/');
if (currid != $userId)
return res.redirect('/');
if (currid === orgid) {
var sql = "DELETE FROM TWEETCONTENTS where TWEETID = ? ";
var values = [
[tweetid]
];
con.query(sql, values, function (err, result) {
if (err) {
console.log(err);
res.redirect("/");
}
else {
// if theres no data or result returns as 0 cancel auth
console.log(result);
res.redirect("/");
}
});
} else {
var sql = "DELETE FROM TWEETS where USERNAME = ? AND TWEETID = ? ";
var values = [
[currid],
[tweetid]
];
con.query(sql, values, function (err, result) {
if (err) {
console.log(err);
res.redirect("/");
}
else {
// if theres no data or result returns as 0 cancel auth
console.log(result);
res.redirect("/");
}
});
}
});
//end add
app.post('/followPerson', function (req, res) {
if (!req.isAuthenticated())
return res.redirect('login');
if (req.user.user_id) {
$userId = req.user.user_id
} else {
//else look for the id in the req.session.passport.user
$userId = req.session.passport.user
}
var follwingid = req.body.userName;
if (follwingid === $userId)
return res.redirect('/');
console.log("making feed of ", $userId);
var sql = "INSERT INTO FOLLOW (follower, following) VALUES ?";
var values = [
[$userId, follwingid]
];
con.query(sql, [values], function (err, result) {
if (err) {
console.log(err);
res.redirect("/");
}
else {
// if theres no data or result returns as 0 cancel auth
console.log(result);
res.redirect("/");
}
});
});
app.get('/makeTweet', function (req, res) {
if (req.isAuthenticated())
res.render("makeTweet");
else res.redirect("/login");
});
app.post('/addTweet', function (req, res) {
if (req.isAuthenticated()) {
const tweet = req.body.tweet;
console.log("tweet", tweet);
if (req.user.user_id) {
$userId = req.user.user_id
} else {
//else look for the id in the req.session.passport.user
$userId = req.session.passport.user
}
console.log($userId, "userId");
var sql = "INSERT INTO TWEETCONTENTS (tweetContent, userName) VALUES ?";
var values = [
[tweet, $userId]
];
console.log(values);
con.query(sql, [values], function (err, result) {
if (err) {
console.log(err);
res.redirect("/");
}
else {
console.log("Number of records inserted: " + result.affectedRows);
console.log(result);
var sql = "INSERT INTO TWEETS (TWEETID, USERNAME) VALUES ?";
var values = [
[result.insertId, $userId]
];
console.log(values);
con.query(sql, [values], function (err, result) {
if (err) {
console.log(err);
res.redirect("/");
}
else {
console.log("Number of records inserted: " + result.affectedRows);
res.redirect("/");
}
});
}
});
}
else res.redirect("/login");
});
app.get('/followers/:personId', function name(req, res) {
if (req.isAuthenticated()) {
console.log("Debug");
if (req.user.user_id) {
$userId = req.user.user_id
} else {
//else look for the id in the req.session.passport.user
$userId = req.session.passport.user
}
console.log($userId, req.params.personId);
if ($userId != req.params.personId)
return res.redirect('/');
var sql = "select * from USERS where USERNAME <> ? and USERNAME in (select FOLLOWER from FOLLOW where FOLLOWING = ?);";
var values = [
[$userId],
[$userId]
];
con.query(sql, values, function (err, result) {
if (err) {
console.log(err);
res.redirect("/login");
}
else {
// if theres no data or result returns as 0 cancel auth
// console.log(result);
console.log(result);
res.render("people2", {
people: result
});
}
});
}
else
res.redirect('/login');
});
app.get('/following/:personId', function name(req, res) {
if (req.isAuthenticated()) {
console.log("Debug");
if (req.user.user_id) {
$userId = req.user.user_id
} else {
//else look for the id in the req.session.passport.user
$userId = req.session.passport.user
}
console.log($userId, req.params.personId);
if ($userId != req.params.personId)
return res.redirect('/');
var sql = "select * from USERS where USERNAME <> ? and USERNAME in (select FOLLOWING from FOLLOW where FOLLOWER = ?);";
var values = [
[$userId],
[$userId]
];
con.query(sql, values, function (err, result) {
if (err) {
console.log(err);
res.redirect("/login");
}
else {
// if theres no data or result returns as 0 cancel auth
// console.log(result);
console.log(result);
res.render("people2", {
people: result
});
}
});
}
else {
console.log("Here");
res.redirect('/login');
}
});
app.get('/retweet/:tweetid', function name(req, res) {
if (req.isAuthenticated()) {
console.log("Debug");
if (req.user.user_id) {
$userId = req.user.user_id
} else {
//else look for the id in the req.session.passport.user
$userId = req.session.passport.user
}
console.log($userId, req.params.tweetid);
var sql = "INSERT INTO TWEETS (TWEETID, USERNAME) VALUES ?";
var values = [
[req.params.tweetid, $userId]
];
console.log(values);
con.query(sql, [values], function (err, result) {
if (err) {
console.log(err);
res.redirect("/");
}
else {
// if theres no data or result returns as 0 cancel auth
console.log(result);
res.redirect("/");
}
});
}
else {
console.log("Here");
res.redirect('/login');
}
});
app.get("/logout", (req, res, next) => {
if (req.isAuthenticated())
req.logout();
res.redirect("/login");
});
app.get('/deleteProfile', function (req, res) {
if (!req.isAuthenticated())
return res.redirect('login');
if (req.user.user_id) {
$userId = req.user.user_id
} else {
//else look for the id in the req.session.passport.user
$userId = req.session.passport.user
}
if ($userId == undefined)
return res.redirect('/');
var sql = "DELETE from USERS where USERNAME = ?";
var values = [
[$userId]
];
con.query(sql, values, function (err, result) {
if (err) {
console.log(err);
res.redirect("/logout");
}
else {
// if theres no data or result returns as 0 cancel auth
// console.log(result);
res.redirect("/logout");
}
});
});
app.listen(3000, function () {
console.log("Server started on port 3000");
});