-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·478 lines (452 loc) · 16.4 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
//Imports
var db = require('./server/config/database')('localhost/faster')
const express = require('express');
const bodyParser = require('body-parser');
const _ = require('lodash');
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
const app = express();
const router = express.Router();
const flash = require('connect-flash');
const passport = require('passport');
const path = require('path');
const ejwt = require('express-jwt');
const moment = require('moment');
const nodemailer = require('nodemailer');
const randomstring = require("randomstring");
const cors = require('cors');
//Express Config
app.use(flash());
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(passport.initialize());
app.use(passport.session());
app.set('superSecret', 'lkmaspokjsafpaoskdpa8asda0s9a'); // secret variable
const LocalStrategy = require('passport-local').Strategy;
const jwtSecret = 'lkmaspokjsafpaoskdpa8asda0s9a';
//NodeMailer config
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: process.env.email, // Your email id
pass: process.env.password // Your password
}
});
//PASSPORT CONFIG
passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'password'
},
function(username, password, done) {
console.log(username);
// pg.connect(connectionString, (error, client) => {
// client.query("SELECT * FROM users WHERE email = '" + username + "'", function(err, result) {
// var user = result.rows[0];
// if (!user) {
// return done("Incorrect credentials", false);
// } else {
// if (user.email == null) {
// return done("Incorrect credentials", false);
// }
// if (user.verified_email == null || user.verified_email == false) {
// return done("Please verify your email first. Check your inbox for our verification email.", false);
// }
// bcrypt.compare(password, user.password, function(err, res) {
// // res == true
// if (res == true) {
// return done(null, user);
// } else {
// return done("Incorrect credentials", false);
// }
// });
// }
// // disconnect the client
// client.end(function(err) {
// if (err) throw err;
// });
// });
// });
}
));
// used to serialize the user for the session
passport.serializeUser(function(user, done) {
done(null, user.id);
// where is this user.id going? Are we supposed to access this anywhere?
});
// used to deserialize the user
passport.deserializeUser(function(id, done) {
// User.findById(id, function(err, user) {});
const results = [];
// Get a Postgres client from the connection pool
// pg.connect(connectionString, (err, client, done) => {
// // SQL Query > Select Data
// const query = client.query('SELECT * FROM users WHERE id = ' + id + ' ORDER BY id ASC;');
// // Stream results back one row at a time
// query.on('row', (row) => {
// results.push(row);
// });
// // After all data is returned, close connection and return results
// query.on('end', () => {
// done(err, user);
// });
// });
});
//User displaying Route
router.get('/users', ejwt({
secret: app.get('superSecret')
}), (req, res) => {
if (!req.user) {
return res.sendStatus(401)
} else if (req.user.role == 'regular') {
return res.status(400).json({ success: false, data: "Insufficient privileges" });
} else {
const results = [];
// Get a Postgres client from the connection pool
// pg.connect(connectionString, (err, client, done) => {
// // Handle connection errors
// if (err) {
// done();
// return res.status(500).json({ success: false, data: err });
// }
// // SQL Query > Select Data
// const query = client.query('SELECT * FROM users ORDER BY id ASC;');
// // Stream results back one row at a time
// query.on('row', (row) => {
// results.push(row);
// });
// // After all data is returned, close connection and return results
// query.on('end', () => {
// done();
// return res.json(results);
// });
// });
}
});
router.post('/auth/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) {
return res.status(400).json({ error: err });
} else {
//user has authenticated correctly thus we create a JWT token
var token = jwt.sign(user, app.get('superSecret'));
console.log(user.role);
return res.json({ success: true, role: user.role, token: token });
}
})(req, res, next);
});
router.post('/auth/register', function(req, res, next) {
const saltRounds = 10;
var ranString = randomstring.generate({
length: 12,
charset: 'alphabetic'
});
const data = {
email: req.body.email,
firstName: req.body.firstName,
lastName: req.body.lastName,
password: bcrypt.hashSync(req.body.password, saltRounds),
verify_token: ranString,
role: 'regular'
};
const results = [];
// Get a Postgres client from the connection pool
// pg.connect(connectionString, (err, client, done) => {
// // Handle connection errors
// if (err) {
// done();
// console.log(err);
// return res.status(500).json({ success: false, data: err });
// }
// // SQL Query > Select Data To Check If Email Already Exists
// client.query("SELECT * FROM users WHERE email = '" + data.email + "' ORDER BY id ASC",
// function(err, result) {
// var user = result.rows[0];
// if (user) {
// return res.status(400).json({ success: false, data: 'User already exists. Please try again.' });
// } else {
// // SQL Query > Insert Data
// client.query('INSERT INTO users(email, password, firstname, lastname, verify_token, role) values($1, $2, $3, $4, $5, $6) RETURNING *', [data.email, data.password, data.firstName, data.lastName, data.verify_token, data.role],
// function(err, result) {
// if (err) {
// return res.status(500).json({ success: false, data: err });
// } else {
// var text = 'Hi ' + data.firstName + '! Welcome to Vue Boilerplate. Please click the following link to verify your email - http://localhost:8090/verify/' + result.rows[0].id + '/' + ranString + '';
// var mailOptions = {
// from: '[email protected]', // sender address
// to: data.email, // list of receivers
// subject: 'Vue Boilerplate - ' + data.email + ' Email Verification', // Subject line
// text: text //, // plaintext body
// // html: '<b>Hello world ✔</b>' // You can choose to send an HTML body instead
// };
// transporter.sendMail(mailOptions, function(error, info) {
// if (error) {
// console.log(error);
// return res.json({ success: false, info: "Something went wrong." });
// } else {
// console.log('Message sent: ' + info.response + text);
// return res.json({ success: true, info: "Registered account and sent verification email." });
// };
// });
// }
// }
// );
// };
// }
// )
// });
});
router.post('/auth/reset', function(req, res) {
var email = req.body.email;
var expiryDate = moment().add(2, 'hours').format("YYYY-MM-DD HH:mm:ss");
var ranString = randomstring.generate({
length: 16,
charset: 'alphabetic'
});
// pg.connect(connectionString, (error, client) => {
// client.query("SELECT * FROM users WHERE email = '" + email + "'", function(err, result) {
// var user = result.rows[0];
// if (!user) {
// return res.status(400).json({ error: 'Email does not match to any account.' });
// } else {
// console.log('reached');
// client.query('UPDATE users SET resetpasstoken=($1), resetpassexpiry=($2) WHERE email=($3)',
// [ranString, expiryDate, email], function(err, result) {
// if (err) {console.log(err);}
// var mailOptions = {
// from: '[email protected]', // sender address
// to: '[email protected]', // list of receivers
// subject: 'Vue Boilerplate - ' + req.body.email + ' Password Reset', // Subject line
// text: 'You are receiving this because you (or someone else) have requested the reset of the password for your account.\n\n' +
// 'Please click on the following link, or paste this into your browser to complete the process:\n\n' +
// 'http://' + req.headers.host + '/reset/' + ranString + '\n\n' +
// 'If you did not request this, please ignore this email and your password will remain unchanged.\n'
// };
// transporter.sendMail(mailOptions, function(error, info) {
// if (error) {
// console.log('Error!!');
// return res.status(400).json({ error: error });
// } else {
// console.log('Message sent: ' + info.response);
// return res.json({ success: true, info: "Password reset email sent." });
// };
// });
// });
// }
// });
// });
});
router.post('/auth/reset/:token', function(req, res) {
console.log(req.body.password);
var password = bcrypt.hashSync(req.body.password, 10);
console.log(password);
console.log(req.params.token);
var token = req.params.token;
// pg.connect(connectionString, (error, client) => {
// client.query("SELECT * FROM users WHERE resetpasstoken = '" + token + "'", function(err, result) {
// var user = result.rows[0];
// if (!user) {
// return res.status(400).json({ error: 'Email does not match to any account.' });
// } else {
// client.query("UPDATE users SET password = '" + password + "', resetpasstoken = NULL, resetpassexpiry = NULL WHERE email = '" + user.email + "'");
// var mailOptions = {
// from: '[email protected]', // sender address
// to: user.email, // list of receivers
// subject: 'Vue Boilerplate - Your Password Has Been Changed', // Subject line
// text: 'Hello,\n\n' +
// 'This is a confirmation that the password for your account ' + user.email + ' has just been changed.\n'
// };
// transporter.sendMail(mailOptions, function(error, info) {
// if (error) {
// return res.status(400).json({ error: error });
// } else {
// console.log('Message sent: ' + info.response);
// return res.json({ success: true, info: "Password reset successful." });
// };
// });
// }
// });
// });
});
router.get('/auth/reset/:token', function(req, res) {
var token = req.params.token;
// pg.connect(connectionString, (error, client) => {
// client.query('SELECT * FROM users WHERE resetpasstoken = ' + token + ' AND resetpassexpiry > ' + Date.now(), function(err, result) {
// var user = result.rows[0];
// if (!user) {
// return res.json({ success: false, message: 'Password reset token is invalid or has expired.' });
// } else {
// return res.json({ success: true, message: 'Please proceed to reset password page.' });
// }
// // disconnect the client
// client.end(function(err) {
// if (err) throw err;
// });
// });
// });
});
router.get('/verify/:id/:token', function(req, res) {
var id = req.params.id;
var token = req.params.token;
// pg.connect(connectionString, (error, client) => {
// client.query("SELECT * FROM users WHERE id = '" + id + "'", function(err, result) {
// var user = result.rows[0];
// if (!user) {
// return res.json({ success: false, message: 'ERROR: User does not exist or something went wrong.' });
// } else {
// if (user.verify_token == token) {
// client.query('UPDATE users SET verified_email = TRUE WHERE id = ' + id + '');
// return res.json({ success: true, message: 'Email is now verified. Please login to get started!' });
// }
// }
// // disconnect the client
// client.end(function(err) {
// if (err) throw err;
// });
// });
// });
});
// DATA ROUTES
router.get('/notes', (req, res) => {
const results = [];
// Get a Postgres client from the connection pool
// pg.connect(connectionString, (err, client, done) => {
// // Handle connection errors
// if (err) {
// done();
// console.log(err);
// return res.status(500).json({ success: false, data: err });
// }
// // SQL Query > Select Data
// const query = client.query('SELECT * FROM notes ORDER BY createdon ASC;');
// // Stream results back one row at a time
// query.on('row', (row) => {
// results.push(row);
// });
// // After all data is returned, close connection and return results
// query.on('end', () => {
// done();
// return res.json(results);
// });
// });
});
router.get('/notes/:note_id', (req, res) => {
const results = [];
// Grab data from the URL parameters
var id = req.params.note_id;
// Get a Postgres client from the connection pool
// pg.connect(connectionString, (err, client, done) => {
// // Handle connection errors
// if (err) {
// done();
// console.log(err);
// return res.status(500).json({ success: false, data: err });
// }
// // SQL Query > Select Data
// const query = client.query("SELECT * FROM notes WHERE id = '" + id + "' ORDER BY id ASC");
// // Stream results back one row at a time
// query.on('row', (row) => {
// results.push(row);
// });
// // After all data is returned, close connection and return results
// query.on('end', function() {
// done();
// return res.json(results);
// });
// });
});
router.post('/notes', ejwt({ secret: 'lkmaspokjsafpaoskdpa8asda0s9a' }), (req, res, next) => {
const results = [];
// Grab data from http request
const data = { text: req.body.text, title: req.body.title, createdBy: req.user.id, createdOn: moment() };
// Get a Postgres client from the connection pool
// pg.connect(connectionString, (err, client, done) => {
// // Handle connection errors
// if (err) {
// done();
// console.log(err);
// return res.status(500).json({ success: false, data: err });
// }
// client.query('INSERT INTO notes(title, text, createdby, createdon) values($1, $2, $3, $4)', [data.title, data.text, data.createdBy, data.createdOn],
// function(err, result) {
// if (err) throw err;
// // just print the result to the console
// return res.json(result);
// // disconnect the client
// client.end(function(err) {
// if (err) throw err;
// });
// });
// });
});
router.put('/notes/:note_id', (req, res, next) => {
const results = [];
// Grab data from the URL parameters
const id = req.params.note_id;
// Grab data from http request
const data = { text: req.body.text, title: req.body.title };
// Get a Postgres client from the connection pool
// pg.connect(connectionString, (err, client, done) => {
// // Handle connection errors
// if (err) {
// done();
// console.log(err);
// return res.status(500).json({ success: false, data: err });
// }
// // SQL Query > Update Data
// client.query('UPDATE notes SET title=($1), text=($2) WHERE id=($3)', [data.title, data.text, id]);
// // SQL Query > Select Data
// const query = client.query("SELECT * FROM notes ORDER BY id ASC");
// // Stream results back one row at a time
// query.on('row', (row) => {
// results.push(row);
// });
// // After all data is returned, close connection and return results
// query.on('end', function() {
// done();
// return res.json(results);
// });
// });
});
router.delete('/notes/:note_id', (req, res, next) => {
const results = [];
// Grab data from the URL parameters
const id = req.params.note_id;
// Get a Postgres client from the connection pool
// pg.connect(connectionString, (err, client, done) => {
// // Handle connection errors
// if (err) {
// done();
// console.log(err);
// return res.status(500).json({ success: false, data: err });
// }
// // SQL Query > Delete Data
// client.query('DELETE FROM notes WHERE id=($1)', [id]);
// // SQL Query > Select Data
// var query = client.query('SELECT * FROM notes ORDER BY id ASC');
// // Stream results back one row at a time
// query.on('row', (row) => {
// results.push(row);
// });
// // After all data is returned, close connection and return results
// query.on('end', () => {
// done();
// return res.json(results);
// });
// });
});
//Server port
app.use('/api', router);
var port = process.env.PORT || 3000;
app.listen(port, function() {
console.log('Boilerplate server listening on port ' + port);
})
// var app = require('./server/config/express'),
// http = require('http').Server(app),
// db = require('./server/config/database')('localhost/faster')
//
// const PORT = process.env.PORT || 3000
//
// http.listen(PORT, () => {
// console.log('Server running on port ' + PORT)
// })