-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathapp.js
More file actions
482 lines (410 loc) · 11.6 KB
/
app.js
File metadata and controls
482 lines (410 loc) · 11.6 KB
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
// Importing required modules and libraries
const express = require("express"); // Express framework for building web applications
const morgan = require("morgan"); // HTTP request logger middleware for Node.js
const axios = require("axios"); // HTTP client for making requests to external APIs
var cookieParser = require('cookie-parser'); // Middleware to parse cookies in the request object
var _ = require('lodash'); // Utility library for various JavaScript functions
const { LOGIN_MAXAGE, VERIFICATION_MAXAGE } = require("./config"); // Importing constants LOGIN_MAXAGE and VERIFICATION_MAXAGE from the config file
require("dotenv").config(); // Loading environment variables from the .env file
// Importing GridFS for storing large files in MongoDB
const Grid = require("gridfs-stream");
const upload = require('./middleware/upload'); // Custom middleware for handling file uploads
// Importing database models
const Account = require('./models/account'); // Model for user accounts
const Profile = require('./models/profile'); // Model for user profiles
const { sendEmailVerificationLink } = require('./utils/emailController'); // Function to send email verification links
// Importing required libraries for database connection
const mongoose = require("mongoose"); // MongoDB object modeling tool
const jwt = require("jsonwebtoken"); // JSON Web Token library for creating and verifying tokens
const app = express(); // Creating an instance of the Express application
// Middleware setup
app.use(morgan("dev")); // Logging HTTP requests to the console in development mode
app.use(express.static('public')); // Serving static files from the 'public' directory
app.use(express.urlencoded({ extended: false })); // Parsing URL-encoded data in the request body
app.use(express.json()); // Parsing JSON data in the request body
app.use(cookieParser()); // Parsing cookies in the request headers
app.set("view engine", "ejs"); // Setting EJS as the view engine for rendering dynamic templates
app.set("view engine", "pug"); // Setting Pug as the view engine (Note: This line is redundant as it overwrites the previous view engine setting)
app.set("json spaces", 2); // Formatting JSON responses with an indentation of 2 spaces
// Connecting to MongoDB
mongoose.connect(process.env.MONGODBURL, { useNewUrlParser: true, useUnifiedTopology: true })
.then((result) => {
console.log('Connected to MongoDB.....');
// Starting the Express server after successfully connecting to MongoDB
app.listen(process.env.PORT || 80, () => {
console.log(`Listening at.... http://localhost:${process.env.PORT || 80}`);
});
})
.catch((err) => console.log(err));
let gfs;
const conn = mongoose.connection;
conn.once("open", function () {
gfs = Grid(conn.db, mongoose.mongo);
gfs.collection("photos");
});
/*
app.get("/", async (req, res) => {
// res.send(req.headers);
let api_url = `${req.headers['x-forwarded-proto']||'http'}://${req.headers.host}/api/info`;
try {
let startDate = new Date();
const response = await axios.get(api_url);
let endDate = new Date();
let mypage = ``;
mypage+=`---- START TIME = ${startDate.toISOString()} <br>`;
mypage+=`---- END TIME= ${endDate.toISOString()} <br>`;
mypage+=`RESPONSE TIME = ${endDate - startDate} <br><br><br>`;
mypage+=`Path /api/info <br><br>`;
mypage+=`Data <br><br>`;
mypage+=`${response.data}`;
res.send(mypage);
} catch (error) {
console.log(error);
res.send(error);
}
});
*/
app.use('/dashboard',require('./server/routes/router'));
app.get("/", (req, res) => {
res.redirect('/login');
});
const autoLogin = (req,res) => {
try{
const token = req.cookies.jwt;
if(token)
{
return jwt.verify(token, process.env.JWTLOGINSECRET);
}
else
{
return false;
}
}catch(error)
{
return false;
}
}
const signVerificationToken = (token) => {
try{
if(token)
{
return jwt.verify(token, process.env.JWTVERIFICATIONSECRET);
}
else
{
return false;
}
}catch(error)
{
return false;
}
}
const createLoginToken = (id) => {
return jwt.sign({ id }, process.env.JWTLOGINSECRET , {
expiresIn: LOGIN_MAXAGE,
});
};
const createVerificationToken = (id) => {
return jwt.sign({ id }, process.env.JWTVERIFICATIONSECRET, {
expiresIn: VERIFICATION_MAXAGE,
});
};
app.get("/login",(req,res)=>{
if(autoLogin(req,res))
{
console.log("Logged IN");
res.redirect('/dashboard');
}
else
{
console.log("Not Logged In");
res.clearCookie("jwt");
res.render('login');
}
});
app.post("/login",async (req,res)=>{
console.log(req.body);
if(req.cookies.jwt)
{
res.send("Logout from existing account");
return;
}
const {username,password,rememberme} = req.body;
if(!username || !password)
{
res.send("Enter Username and Password");
return;
}
try {
const user = await Account.login(username,password);
if(user)
{
// res.send(user);
// Vaid Username and Password
const LToken = createLoginToken(user._id);
if(rememberme)
{
// Set Login Cookie to 5 Days
res.cookie("jwt", LToken, { httpOnly: true, maxAge: LOGIN_MAXAGE * 1000 });
}
else
{
// Set Login Cookie to session only
res.cookie("jwt", LToken, { httpOnly: true});
}
res.send({code:'007',msg:"Logged in Successfully"});
}
} catch (error) {
console.log(error.message);
res.send("Invalid Username or Password");
}
});
app.get("/signup",(req,res)=>{
res.render('signup');
});
app.post("/signup",async (req,res)=>{
const {username,password,email} = req.body;
if(!username || !password || !email)
{
res.status(404);
res.send("Enter Username , Password, Email");
return;
}
try
{
let curdat = new Date();
let mydt = curdat.toLocaleString("en-US",{timeZone: 'Asia/Kolkata'});
const user = await Account.create({
username:username,
password:password,
email:email,
createdDate:mydt
});
const profile = await Profile.create({userid:user._id});
if(user)
{
console.log("Account Created Successfully ... ");
// console.log(user_id);
// res.send(user_id);
const VToken = createVerificationToken(user._id);
sendEmailVerificationLink(username,email,VToken,req, (response) => {
console.log("==== app.js ======");
console.log(response);
if (response == 200) {
// res.send("Verification Email Successfully Sent!");
res.send({code:'010',msg:"Signup Successfull & Verification Email Successfully Sent!"});
} else {
// Account Created Successfully But Failed to Send Verification Email
// res.sendStatus(404);
res.send({code:'011',msg:"Signup Successfull but failed to send verification email"});
}
});
}
else
{
console.log("Something Went Wrong!");
res.sendStatus(404);
}
}catch(error)
{
console.log(error);
let outmsg = [];
if (error.code && error.code === 11000)
{
if('username' in error.keyPattern)
{
outmsg.push('Username is Already In Use');
}
if('email' in error.keyPattern)
{
outmsg.push('Email is Already In Use');
}
}
if(error.errors)
{
if(error.errors['username'])
{
console.log(error.errors['username'].message);
outmsg.push(error.errors['username'].message);
}
if(error.errors['email'])
{
console.log(error.errors['email'].message);
outmsg.push(error.errors['email'].message);
}
if(error.errors['password'])
{
console.log(error.errors['password'].message);
outmsg.push(error.errors['password'].message);
}
}
res.status(404);
res.send({"msg":"Please enter valid information","errors":outmsg});
}
});
app.get("/forgot-password",(req,res)=>{
if(_.isEmpty(req.query) == false)
{
// With Parameters
console.log("With Parameters");
console.log(req.query);
}
else
{
// Without Parameters
console.log("No Parameters");
console.log("EMPTY");
}
});
app.get("/api/public/profile/photo/:photoId",async (req,res)=>{
try {
let check = mongoose.isValidObjectId(req.params.photoId);
if(check)
{
const file = await gfs.files.findOne(mongoose.Types.ObjectId(req.params.photoId));
if(file)
{
const readStream = gfs.createReadStream(file.filename);
readStream.pipe(res);
}
else
{
res.status(404);
res.send("No Profile Pic Found");
}
}
else { res.send("Invalid")}
} catch (error) {
console.log(error);
res.status(500);
res.send("Something Went Wrong Try Again Later");
}
});
app.post("/reset-password",(req,res)=>{
});
app.get("/verify",async (req,res)=>{
const {token} = req.query;
if(token)
{
console.log(token);
const decodedToken = signVerificationToken(token);
console.log(decodedToken);
if(decodedToken)
{
console.log("Successfull. . . ");
const user = await Account.findOne({_id:decodedToken.id});
console.log(user);
if(user)
{
if(user.status == 'active')
{
res.send("User Already Verified");
}
else
{
const transction = await Account.updateOne({_id:decodedToken.id},{status:'active'});
console.log(transction);
if(transction.acknowledged)
{
res.send("Account Verified Successfully");
}
else
{
res.sendStatus(404);
}
}
}
else
{
res.sendStatus(404);
}
}
else
{
console.log("Invalid Token");
res.sendStatus(404);
}
}
else
{
res.send("Invalid Verification URL");
}
});
/*
app.get("/dashboard", async (req, res) => {
if(!req.cookies.jwt)
{
//JWT Token Not Present
res.redirect("/login");
return;
}
const decodedToken = autoLogin(req,res);
if(decodedToken)
{
console.log(decodedToken);
const user = await Account.findOne({_id:decodedToken.id});
console.log(user);
if(user)
{
if(user.status=='verification')
{
// Email Not Verified
res.send("Welcome to Project Linked List | Email under Verification | Not Active");
}
else
{
//Active
res.send("Welcome to Project Linked List | Account Active");
}
}
else
{
res.sendStatus(404);
}
}
else
{
// Invalid Token
res.redirect("/login");
}
});
*/
app.get("/logout", async(req,res)=>{
res.clearCookie('jwt');
res.redirect("/login");
});
//User Public Page
app.get('/:username',(req,res,next)=>{
console.log("============ USER SEARCH ==========");
const {username} = req.params;
//Finding User in Database
Account.findOne({username:username})
.then(async (data)=>{
if(!data)
{
//Usernot found
next();
}
else
{
//Userfound. Now get his profile to search
await Profile.updateOne({userid:data._id},{$inc: {totalViews:1}});
Profile.findOne({userid:data._id})
.then((profile_data)=>{
// res.send(profile_data);
res.render('userpage/index.ejs',{ profile:profile_data,myusername:data.username,myemail:data.email});
})
.catch((err)=>{
next();
})
}
})
.catch((err)=>{
next();
})
});
app.use((req,res)=>{
// res.send("We coudnout found anything!");
res.status(404);
res.render("404page");
});