-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
402 lines (366 loc) · 9.92 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
const express = require('express');
const mongoose = require('mongoose');
const handlebars = require('express-handlebars');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const expressValidator = require('express-validator');
const MongoDBStore = require('connect-mongodb-session')(session);
const path = require('path');
const favicon = require('serve-favicon');
const sha256 = require('sha256');
var DB_URL = "mongodb://localhost/devtracker";
var app = express();
mongoose.Promise = global.Promise; //this is very slow so change to bluebird for example
mongoose.connect(DB_URL, {
useMongoClient: true
});
// mongoose.connection.openUri('mongodb://localhost/devtracker'); use this if deprecated msg comes up again
var store = new MongoDBStore({
uri: DB_URL,
collection: 'sessions'
});
// Catch errors
store.on('error', function(error) {
if (error) console.log(error);
});
app.use(express.static(__dirname));
app.use(favicon(path.join(__dirname, 'public', 'favi.ico')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use(expressValidator());
app.use(session({
key: 'user_sid',
secret: 'znxbcuzxxiyekjnadc',
store: store,
resave: false,
saveUninitialized: false,
cookie: {
expires: 6000000, //10mins if u remove one zero
// secure: true //ENABLE when HTTPS is setup!!
}
}));
app.use(function(req, res, next) {
res.locals.LoggedIn = (req.session.user && req.cookies.user_sid) ? true : false;
next();
});
app.engine('handlebars', handlebars({
defaultLayout: 'main'
})); //View engine (Handlebars)
app.set('view engine', 'handlebars');
app.listen(8080, function() {
console.log("server running on port 8080!");
});
// middleware function to check for logged-in users
var sessionChecker = (req, res, next) => {
if (!req.session.user || !req.cookies.user_sid) {
res.redirect('/login');
} else {
next();
}
};
app.get('/logout', function(req, res) {
req.session.destroy();
res.redirect('/login');
});
app.get('/', sessionChecker, function(req, res) {
getCurrentUser(req.session, function(result) {
Project.find({
owner: req.session.user
}).sort({
last_updated: -1
}).exec(function(err, userProjects) {
res.render('dashboard', {
title: "DevTracker - Home",
user: result,
projects: userProjects,
stylesheet: "dashboard"
});
});
})
});
app.get('/login', function(req, res) {
if (!req.session.user) { //if user isnt logged in
res.render('login', {
title: "DevTracker - Login",
stylesheet: "login"
});
} else {
res.redirect('/');
}
});
app.get('/signup', function(req, res) {
if (!req.session.user) { //if user isnt logged in
res.render('signup', {
title: "DevTracker - Signup",
stylesheet: "login"
});
} else {
res.redirect('/');
}
});
app.post('/login', (req, res) => {
var email = req.body.email.toLowerCase();
var password = sha256(req.body.password);
User.findOne({
'email': email
}, function(err, user) {
if (user == null) {
var errors = [{
msg: '- The email you entered is incorrect.'
}]
res.render('login', {
errors: errors,
stylesheet: "login"
});
} else if (password == user.password) { //login successful!
var user_id = user._id;
req.session.user = user_id;
res.redirect('/');
} else {
var errors = [{
msg: '- The password you entered is incorrect.'
}]
res.render('login', {
errors: errors,
stylesheet: "login"
});
}
});
});
app.post('/signup', (req, res) => {
req.checkBody('first_name', '- Invalid first name').notEmpty().matches(/^[A-Za-z0-9 _-]+$/, "i");
req.checkBody('last_name', '- Invalid last name').notEmpty().matches(/^[A-Za-z0-9 _-]+$/, "i");
req.checkBody('email', '- Email you entered is invalid.').isEmail();
req.checkBody('email', '- Email must be 4-100 characters long.').len(4, 100);
req.checkBody('password', '- Password must be 6-100 characters long & contain no special characters').matches(/^[a-z0-9_-]{6,100}$/, "i");
req.checkBody('password2', '- Passwords do not match').equals(req.body.password);
req.getValidationResult().then(function(result) {
if (result.isEmpty()) { //if no errors then save new user else output validation errors
var user = {
first_name: req.body.first_name,
last_name: req.body.last_name,
email: req.body.email.toLowerCase(),
password: sha256(req.body.password)
}
var data = new User(user);
data.save()
.then((savedUser) => {
var user_id = savedUser._id;
req.session.user = user_id;
res.redirect('/');
}).catch((error) => { //check this code again because it doesnt tell the user when the email has been duplicated
if (error.code == 11000) {
result.array().push({
msg: "- Email address has already been taken."
});
res.render('signup', {
errors: result.array(),
stylesheet: "login"
});
}
});
} else {
res.render('signup', {
errors: result.array(),
stylesheet: "login"
});
}
});
});
app.get('/project/:id', sessionChecker, function(req, res) {
Project.findOne({
owner: req.session.user,
_id: req.params.id
}, function(err, project) {
if (project == null) {
res.status(404).send("project doesn't exist!");
} else {
res.render('features', {
project: project,
stylesheet: "features"
});
}
});
});
app.post('/addproject', sessionChecker, function(req, res) {
var project = new Project({
owner: req.session.user,
name: req.sanitize('projectName').trim(),
colour: req.body.projectColour
});
project.save()
.then((savedProject) => {
res.redirect('/');
}).catch((error) => {
console.log(error);
res.redirect('/');
});
});
app.get('/project/:id/addfeature', sessionChecker, function(req, res) {
Project.findOne({
owner: req.session.user,
_id: req.params.id
}, function(err, project) {
if (project == null) {
res.status(404).send("project doesn't exist!");
} else {
res.render('feature-add', {
project: project,
stylesheet: "features"
});
}
});
});
app.post('/project/:id/addfeature', sessionChecker, function(req, res) {
//PERFORM VALIDATION CHECKS ON ALL FIELDS! if all good then run code below
// req.checkBody('title', '- ').notEmpty().matches(, "i");
// req.checkBody('description', '- ').matches(, "i");
// req.checkBody('tag', '- ').matches(, "i");
req.getValidationResult().then(function(result) {
if (result.isEmpty()){ //if no errors then save new user else output validation errors
var featureData = {
title: req.sanitize('title').escape().trim(),
description: req.sanitize('description').escape().trim(),
tag: req.sanitize('tag').escape().trim(),
status: req.body.status,
dueDate: req.body.dueDate
}
Project.findOneAndUpdate({
owner: req.session.user,
_id: req.params.id
}, {
last_updated: Date.now(),
$push: {
features: featureData
}
}, function(err, updatedProject) {
if (err) console.log(err);
res.redirect('/project/' + updatedProject.id);
});
} else {
res.redirect('/project/' + req.params.id);
}
});
});
app.get('/project/:id/delete/:featureid', function(req,res){
Project.findOneAndUpdate({
owner: req.session.user,
_id: req.params.id
} , {
"$pull": { "features": { "_id": req.params.featureid}}}
, function(err, updatedProject) {
if (err) console.log(err);
res.redirect('/project/' + updatedProject.id);
});
});
app.get('*', function(req, res){
res.send('what???', 404);
});
function getCurrentUser(session, callback) {
User.findOne({
'_id': session.user
}, function(err, user) {
callback(user);
});
}
//--------------------------------------MONGOOSE STUFF (MOVE TO SEPERATE FILE EVENTUALLY) ----------------------------------
var Schema = mongoose.Schema;
var userSchema = new Schema({
first_name: {
type: String,
required: true
},
last_name: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
date_created: {
type: Date,
default: Date.now
},
}, {
collection: 'users'
});
var User = mongoose.model('User', userSchema);
var featureSchema = new Schema({
title: String,
description: String,
colour: String,
tag: String,
dueDate: String,
status: String,
isPinned: {
type: Boolean,
default: false
}
});
var issueSchema = new Schema({
title: String,
description: String,
status: String,
});
var projectSchema = new Schema({
owner: {
type: String,
required: true
},
name: {
type: String,
required: true
},
colour: {
type: String,
default: "white"
},
last_updated: {
type: Date,
default: Date.now
},
date_created: {
type: Date,
default: Date.now
},
features: [featureSchema],
issues: [issueSchema]
}, {
collection: 'projects'
});
var Project = mongoose.model('Project', projectSchema);
/*
var project = new Project({
owner: "Owner id",
name: "PName",
features: [{
title: "FTitle",
description: "FDescription",
colour: "FColour",
tag: "Ftag",
status: "FStatus",
isPinned: false
}],
issues: [{
title: "ETitle",
description: "EDscription",
status: "EStatus"
}]
});
project.save()
.then((savedProject) => {
console.log(savedProject);
}).catch((error) => { //c-heck this code again because it doesnt tell the user when the email has been duplicated
console.log(error); //email duplication error code is 11000 (error.code)
});
*/