-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
549 lines (400 loc) · 15.2 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
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
var express = require('express');
var fs = require("fs");
var cookieParser = require('cookie-parser');
var session = require('express-session');
var bodyParser = require("body-parser");
var app = express();
/**tell express to use body-parser so that it can read POST forms*/
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(cookieParser());
/** session configuration **/
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
cookie: {
secure: false
}
}));
/** tell expressJs to use the "app" folder to serve all the files */
app.use(express.static('public'));
/** handle "/signin" url */
app.post('/signin', function (request, response) {
//write the logic to verify user email and password
//var email = request.body.email;
//var pwd = request.body.pwd;
console.log(JSON.stringify(request.body));
var data = request.body;
var uName = data.userName;
var pwd = data.password;
console.log("uName=" + uName + " and pwd=" + pwd);
//check in file data
fs.readFile(__dirname + "/data/userList.json", 'utf-8', function (err, data) {
var jsObjectForData = JSON.parse(data);
var usersList = jsObjectForData.userList;
var recordFound = false;
var name = "";
for (var i = 0; i < usersList.length; i++) {
console.log("checking with uName =" + usersList[i].userName + "pwd=" + usersList[i].password);
if ((uName == usersList[i].userName) && (pwd == usersList[i].password)) {
recordFound = true;
name = usersList[i].userName;
console.log("Yeah!! its matched..");
}
if (recordFound) break;
}
if (recordFound) {
request.session.isLoggedIn = true;
request.session.username = uName;
// request.session.name = name;
//response.send("Success");
//response.redirect('/dashboard');
var success = {};
success.loginSuccess = true;
success.userinfo = {};
success.userinfo.name = name;
response.send(success);
} else {
var failure = {};
failure.loginSuccess = false;
response.send(failure);
}
});
});
/** handle "/register" url */
app.post('/register', function (request, response) {
console.log(JSON.stringify(request.body));
var email = request.body.email;
var userName = request.body.userName;
// var name = data.name;
//
// var newUser = {};
// newUser.email = email;
// newUser.pwd = pwd;
// newUser.name = name;
//console.log("email=" + email + " and pwd=" + password);
fs.readFile(__dirname + "/data/userList.json", 'utf-8', function (err, data) {
/**process the file data here
may be validate incoming user data with existing data
**/
console.log('file data=' + data);
var jsObjectForData = JSON.parse(data);
// now verify in the json object, whether this user is already registred with the email or not
//you have to make its not a duplicate email id
var usersList = jsObjectForData.userList;
var recordFound = false;
var name = "";
if (usersList.length == 0) {
recordFound = false;
} else {
for (var i = 0; i < usersList.length; i++) {
console.log("checking with email =" + usersList[i].email + "userName=" + usersList[i].userName);
if ((email == usersList[i].email) && (userName == usersList[i].userName)) {
recordFound = true;
userName = usersList[i].userName;
console.log("Yeah!! its matched..");
}
if (recordFound) break;
}
}
if (!recordFound) {
jsObjectForData.userList.push(request.body);
console.log(JSON.stringify(jsObjectForData));
var stringFile = JSON.stringify(jsObjectForData);
fs.writeFile(__dirname + "/data/userList.json", stringFile);
var success = {};
success.registerSuccess = true;
success.userinfo = {};
success.userinfo.name = userName;
response.send(success);
} else {
var failure = {};
failure.registerSuccess = false;
response.send(failure);
}
});
});
app.get('/dashboard', function (request, response) {
if (request.session.isLoggedIn) {
var htmlText = `<!DOCTYPE html>
<html>
<head>
<title>Dashboard</title>
</head>
<body>
Welcome ` + request.session.name + ` <a href="/logout"> Logout </a>
</body>
</html>`;
response.send(htmlText);
} else {
response.sendFile(__dirname + "/app/invalidSession.html");
}
});
app.get('/logout', function (req, res) {
console.log('/logout');
req.session.destroy();
res.end();
});
//app.get('/status',function(req,res){
// if(request.session.isLoggedIn){
// res.send(true);
// }
//})
app.get('/getUserInfo', function (request, response) {
if (request.session.isLoggedIn) {
var session = {};
session.log = request.session.isLoggedIn;
console.log(session.log);
// var name = request.session.name;
// var data = {};
// data.name = name;
response.send(session);
response.end();
} else {
response.send(false);
response.end();
}
});
var selectUserFromDb = function (usersList, userName) {
console.log('selectuserList', usersList);
console.log('selectUser', userName);
var userObject = null;
var recordFound = false;
for (var i = 0; i < usersList.length; i++) {
console.log("checking with userName =" + usersList[i].userName);
if ((userName == usersList[i].userName)) {
recordFound = true;
userObject = usersList[i];
console.log("Yeah!! its matched..");
}
if (recordFound) break;
}
return userObject;
}
app.get('/contactsList', function (request, response) {
console.log("entered contactlist");
//response.sendFile(__dirname + "/data/contactList.json");
console.log(request.session);
if (request.session.isLoggedIn) {
var username = request.session.username;
console.log("userName contactlist", username);
fs.readFile(__dirname + "/data/userList.json", 'utf-8', function (err, data) {
var jsObjectForData = JSON.parse(data);
var usersList = jsObjectForData.userList;
console.log('usersList', usersList);
var userObject = selectUserFromDb(usersList, username);
if (userObject != null) {
var success = {};
success.success = true;
success.contactList = userObject.contacts;
console.log("success.contactList", success.contactList)
response.send(success);
} else {
var failure = {};
failure.sucess = false;
response.send(failure);
}
});
} else {
var failure = {};
failure.sucess = false;
response.send(failure);
}
});
app.post('/addContact', function (request, response) {
console.log("entered add contact");
if (request.session.isLoggedIn) {
var userName = request.session.username;
console.log("usename", userName);
fs.readFile(__dirname + "/data/userList.json", 'utf-8', function (err, data) {
var jsObjectForData = JSON.parse(data);
var usersList = jsObjectForData.userList;
var userObject = selectUserFromDb(usersList, userName);
if (userObject != null) {
console.log('file data' + data);
var fileJsonData = JSON.parse(data);
var newContact = request.body;
newContact.id = userObject.contacts.length + 1;
console.log("newContact=" + JSON.stringify(newContact));
userObject.contacts.push(newContact);
var stringFile = JSON.stringify(jsObjectForData);
fs.writeFile(__dirname + "/data/userList.json", stringFile);
//fs.close();
var success = {};
success.success = true;
success.contactList = userObject.contacts;
response.send(success);
} else {
var failure = {};
failure.sucess = false;
response.send(failure);
}
});
} else {
var failure = {};
failure.sucess = false;
response.send(failure);
}
console.log("incoming data=" + JSON.stringify(request.body));
});
app.post('/deleteContact', function (request, response) {
console.log("incoming data=" + JSON.stringify(request.body));
if (request.session.isLoggedIn) {
var userName = request.session.username;
fs.readFile(__dirname + "/data/userList.json", 'utf-8', function (err, data) {
var jsObjectForData = JSON.parse(data);
var usersList = jsObjectForData.userList;
var userObject = selectUserFromDb(usersList, userName);
if (userObject != null) {
var deletedContact = request.body;
var index = -1;
for (var i = 0; i < userObject.contacts.length; i++) {
if (userObject.contacts[i].id == deletedContact.id) {
index = i;
}
}
console.log("index=" + index);
if (index >= 0) {
var newContactsList = userObject.contacts.splice(index, 1);
var stringFile = JSON.stringify(jsObjectForData);
console.log("after Delete=" + stringFile);
fs.writeFile(__dirname + "/data/userList.json", stringFile);
//response.send(stringFile);
var success = {};
success.success = true;
//success.contactList = userObject.contacts;
response.send(success);
} else {
response.send(JSON.stringify(jsObjectForData));
}
} else {
var failure = {};
failure.sucess = false;
response.send(failure);
}
});
} else {
var failure = {};
failure.sucess = false;
response.send(failure);
}
});
app.get('/getContact', function (request, response) {
var id = request.query.id;
console.log("id=" + id);
if (request.session.isLoggedIn) {
var userName = request.session.username;
fs.readFile(__dirname + "/data/userList.json", 'utf-8', function (err, data) {
var jsObjectForData = JSON.parse(data);
var usersList = jsObjectForData.userList;
var userObject = selectUserFromDb(usersList, userName);
if (userObject != null) {
console.log('file data' + data);
var foundContact = {};
for (var i = 0; i < userObject.contacts.length; i++) {
if (userObject.contacts[i].id == id) {
foundContact = userObject.contacts[i];
}
}
var stringFile = JSON.stringify(foundContact);
response.send(stringFile);
} else {
var failure = {};
failure.sucess = false;
response.send(failure);
}
});
} else {
var failure = {};
failure.sucess = false;
response.send(failure);
}
});
app.post('/updateContact', function (request, response) {
console.log("incoming data=" + JSON.stringify(request.body));
if (request.session.isLoggedIn) {
var userName = request.session.username;
fs.readFile(__dirname + "/data/userList.json", 'utf-8', function (err, data) {
console.log('file data' + data);
var fileJsonData = JSON.parse(data);
var usersList = fileJsonData.userList;
var userObject = selectUserFromDb(usersList, userName);
if (userObject != null) {
var updatedContact = request.body;
for (var i = 0; i < userObject.contacts.length; i++) {
if (userObject.contacts[i].id == updatedContact.id) {
userObject.contacts[i] = updatedContact;
break;
}
}
var stringFile = JSON.stringify(fileJsonData);
fs.writeFile(__dirname + "/data/userList.json", stringFile);
//fs.close();
response.send(userObject.contacts);
} else {
response.send({});
}
});
} else {
response.send({});
}
});
app.get('/forgotPswd', function (request, response) {
var email = request.query.email;
console.log("entered forgotPswd", email);
fs.readFile(__dirname + "/data/userList.json", 'utf-8', function (err, data) {
var fileJsonData = JSON.parse(data);
var recordFound = false;
var usersList = fileJsonData.userList;
for (var i = 0; i < usersList.length; i++) {
console.log("checking with email =" + usersList[i].email);
if ((email == usersList[i].email)) {
recordFound = true;
var userObject = usersList[i];
console.log("Yeah!! its matched..");
}
if (recordFound) break;
}
if (recordFound) {
var success = {};
success.status = true;
success.username = userObject.userName;
console.log("successObj", success);
response.send(success);
response.end();
} else {
var failure = {};
failure.status = false;
response.send(failure);
response.end();
}
});
});
app.post('/updatePswd', function (req, res) {
console.log("entered updatePswd", req.body);
var username = req.body.username;
fs.readFile(__dirname + "/data/userList.json", 'utf-8', function (err, data) {
console.log('file data' + data);
var fileJsonData = JSON.parse(data);
var usersList = fileJsonData.userList;
var userObject = selectUserFromDb(usersList, username);
if (userObject != null) {
console.log("userObj", userObject);
userObject.password = req.body.pswd;
console.log("afterchange", userObject);
console.log(JSON.stringify(fileJsonData));
var updated = JSON.stringify(fileJsonData);
fs.writeFile(__dirname + "/data/userList.json", updated);
res.send("success");
res.end();
} else {
res.send("failed to change");
}
});
});
app.listen(3001, function () {
console.log("server started at port 3000");
});