Skip to content

Commit 982b5db

Browse files
authored
Merge pull request #61 from oslabs-beta/cleanup
cleaned up server-side
2 parents 7fa4102 + a745671 commit 982b5db

File tree

7 files changed

+52
-64
lines changed

7 files changed

+52
-64
lines changed

server/app.js

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ const path = require('path');
33
const cors = require('cors');
44
const colors = require('colors');
55

6-
// Routers
7-
// sign up for sysadmin to add new users to their account
86
const signupRouter = require('./routes/signupRouter');
9-
// sign up for new sysadmin users
7+
108
const signupSysAdminRouter = require('./routes/signupSysAdminRouter');
119
const loginRouter = require('./routes/loginRouter');
1210
const adminRouter = require('./routes/adminRouter');
@@ -19,9 +17,9 @@ const settingsRouter = require('./routes/settingsRouter');
1917

2018
const app = express();
2119

22-
app.use(express.json()); // parses the request body
23-
app.use(express.urlencoded({ extended: true })); // parses urlencoded payloads
24-
app.use(cors()); // enables ALL cors requests
20+
app.use(express.json());
21+
app.use(express.urlencoded({ extended: true }));
22+
app.use(cors());
2523

2624
app.use('/test', (req, res) => {
2725
res.status(200).json({
@@ -31,30 +29,18 @@ app.use('/test', (req, res) => {
3129

3230
app.use('/settings', settingsRouter);
3331
app.use('/init', initRouter);
34-
// sign up for sysadmin to add new users to their account
3532
app.use('/signup', signupRouter);
36-
// sign up for new sysadmin users
37-
// app.use('/signupsysadmin', signupSysAdminRouter);
3833
app.use('/login', loginRouter);
3934
app.use('/admin', adminRouter);
4035
app.use('/account', accountRouter);
4136
app.use('/api', apiRouter);
4237
app.use('/db', dbRouter);
4338
app.use('/logout', logoutRouter);
4439

45-
// Unknown Endpoint Error Handler
4640
app.use('/', (req, res) => {
47-
/*
48-
Reads the current URL (explains why electron crashes)
49-
const url = new URL(`${req.protocol}://${req.get('host')}${req.originalUrl}`);
50-
console.log('current url',url);
51-
*/
52-
// for development purposes, so we don't have to reopen electron everytime
5341
return res.status(404).redirect('/');
54-
// return res.status(404).json('404 Not Found')
5542
});
5643

57-
// Global Error Handler
5844
app.get('/', (req, res, next, err) => {
5945
const defaultErr = {
6046
log: 'Express error handler caught unknown middleware error',
@@ -66,3 +52,9 @@ app.get('/', (req, res, next, err) => {
6652
});
6753

6854
module.exports = app;
55+
56+
/*
57+
Reads the current URL (explains why electron crashes)
58+
const url = new URL(`${req.protocol}://${req.get('host')}${req.originalUrl}`);
59+
console.log('current url',url);
60+
*/

server/controllers/apiController.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
* @module API Controller
33
* @description Contains middleware that sends emails to user for container issues and signup information
44
*/
5-
const nodemailer = require('nodemailer');
6-
const email = require('../../security/email');
5+
const nodemailer = require("nodemailer");
6+
const email = require("../../security/email");
77

88
const apiController = {};
99

10-
// create transporter object
11-
// make sure these values are filled out in email.js
10+
// create transporter object to make sure these values are filled out in email.js
1211
const transporter = nodemailer.createTransport({
1312
host: email.host,
1413
port: email.port,
@@ -24,7 +23,7 @@ apiController.sendEmailAlert = (req, res, next) => {
2423
const { email, containerName, time, date, stopped } = req.body;
2524
let emailBody;
2625

27-
if (stopped === 'true') {
26+
if (stopped === "true") {
2827
emailBody = `
2928
<h2>Alert: ${containerName} has stopped!</h2>
3029
<h3>Container <b>${containerName}</b> stopped running at <b>${time}</b> on <b>${date}</b>.</h3>
@@ -45,9 +44,9 @@ apiController.sendEmailAlert = (req, res, next) => {
4544
}
4645

4746
const mailDetails = {
48-
47+
4948
to: email,
50-
subject: 'Docketeer: Container Issue',
49+
subject: "Docketeer: Container Issue",
5150
html: `${emailBody}`,
5251
};
5352

@@ -60,7 +59,7 @@ apiController.sendEmailAlert = (req, res, next) => {
6059
return next({
6160
log: `Error in apiController sendEmailAlert: ${err}`,
6261
message: {
63-
err: 'An error occured creating new user in database. See apiController.sendEmailAlert.',
62+
err: "An error occured creating new user in database. See apiController.sendEmailAlert.",
6463
},
6564
});
6665
});
@@ -71,9 +70,9 @@ apiController.signupEmail = (req, res, next) => {
7170
const { email, username, password } = req.body;
7271

7372
const mailDetails = {
74-
73+
7574
to: email,
76-
subject: 'Docketeer: Account Details',
75+
subject: "Docketeer: Account Details",
7776
html: `
7877
<h1>Welcome to Docketeer</h1>
7978
<p>We are so excited to have you onboard!</p>
@@ -94,7 +93,7 @@ apiController.signupEmail = (req, res, next) => {
9493
return next({
9594
log: `Error in apiController signupEmail: ${err}`,
9695
message: {
97-
err: 'An error occured creating new user in database. See apiController.signupEmail.',
96+
err: "An error occured creating new user in database. See apiController.signupEmail.",
9897
},
9998
});
10099
});

server/controllers/bcryptController.js

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* @module Bcrypt Controller
33
* @description Contains middleware that encrypts password before storing in database and compares a user's inputted password to their stored password
44
*/
5-
const db = require('../models/cloudModel');
6-
const bcrypt = require('bcryptjs');
5+
const db = require("../models/cloudModel");
6+
const bcrypt = require("bcryptjs");
77

88
const bcryptController = {};
99

@@ -12,69 +12,80 @@ bcryptController.hashPassword = (req, res, next) => {
1212
const { password } = req.body;
1313
const saltRounds = 10;
1414

15-
bcrypt.hash(password, saltRounds)
15+
bcrypt
16+
.hash(password, saltRounds)
1617
.then((hash) => {
1718
res.locals.hash = hash;
1819
return next();
1920
})
2021
.catch((err) => {
2122
return next({
2223
log: `Error in bcryptController hashPassword: ${err}`,
23-
message: { err: 'An error occured creating hash with bcrypt. See bcryptController.hashPassword.' },
24+
message: {
25+
err: "An error occured creating hash with bcrypt. See bcryptController.hashPassword.",
26+
},
2427
});
2528
});
2629
};
2730

2831
// Hash new user password with bCrypt - User updated password
2932
bcryptController.hashNewPassword = async (req, res, next) => {
30-
3133
// if there is an error property on res.locals, return next(). i.e., incorrect password entered
32-
if (Object.prototype.hasOwnProperty.call(res.locals, 'error')){
34+
if (Object.prototype.hasOwnProperty.call(res.locals, "error")) {
3335
return next();
3436
}
3537
// else bCrypt the new password and move to next middleware
3638
const { newPassword } = req.body;
3739
const saltRounds = 10;
3840

39-
await bcrypt.hash(newPassword, saltRounds)
41+
await bcrypt
42+
.hash(newPassword, saltRounds)
4043
.then((hash) => {
4144
res.locals.hash = hash;
4245
return next();
4346
})
4447
.catch((err) => {
4548
return next({
4649
log: `Error in bcryptController hashNewPassword: ${err}`,
47-
message: { err: 'An error occured creating hash with bcrypt. See bcryptController.hashNewPassword.' },
50+
message: {
51+
err: "An error occured creating hash with bcrypt. See bcryptController.hashNewPassword.",
52+
},
4853
});
4954
});
5055
};
5156

5257
/**
5358
* @description hashes the locals property cookie. Creates a column in the database to store the hashed cookie
5459
*/
55-
bcryptController.hashCookie = (req, res, next) =>{
60+
61+
bcryptController.hashCookie = (req, res, next) => {
5662
const { role_id, username } = res.locals.user;
5763
const saltRounds = 10;
5864
if (role_id === 1) {
59-
bcrypt.hash(res.locals.cookie, saltRounds)
65+
bcrypt
66+
.hash(res.locals.cookie, saltRounds)
6067
.then((hash) => {
6168
res.locals.user.token = hash;
62-
db.query('ALTER TABLE users ADD COLUMN IF NOT EXISTS token varchar(250)');
63-
db.query('UPDATE users SET token=$1 WHERE username=$2', [res.locals.user.token, username]);
69+
db.query(
70+
"ALTER TABLE users ADD COLUMN IF NOT EXISTS token varchar(250)"
71+
);
72+
db.query("UPDATE users SET token=$1 WHERE username=$2", [
73+
res.locals.user.token,
74+
username,
75+
]);
6476
return next();
6577
})
6678
.catch((err) => {
6779
return next({
6880
log: `Error in bcryptController hashCookeis: ${err}`,
69-
message: { err: 'An error occured creating hash with bcrypt. See bcryptController.hashCookies.' },
81+
message: {
82+
err: "An error occured creating hash with bcrypt. See bcryptController.hashCookies.",
83+
},
7084
});
7185
});
7286
} else {
7387
return next();
7488
}
7589
};
7690

77-
78-
79-
80-
module.exports = bcryptController;
91+
module.exports = bcryptController;

server/controllers/cookieController.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
* @description Contains middleware that stores the user id in a HTTP-only cookie and sets HTTP-only cookie specifically for admins
44
*/
55

6-
7-
86
const cookieController = {};
97

108
// store the user id in a cookie

server/controllers/userController.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,14 @@ userController.verifyUser = (req, res, next) => {
9393
const getUser = `SELECT * FROM users WHERE username='${username}';`;
9494

9595
db.query(getUser)
96-
.then(async (data) => {
97-
// console.log('hello');
98-
// console.log(password);
99-
// console.log(data.rows[0].password);
100-
const match = await bcrypt.compare(password, data.rows[0].password);
101-
// console.log(match);
96+
.then(async (data) => {
97+
const match = await bcrypt.compare(password, data.rows[0].password);
10298
if (data.rows[0] && match) {
10399
res.locals.user = data.rows[0];
104100
return next();
105101
} else {
106102
res.locals.error = 'Incorrect username or password.';
107103
delete res.locals.user;
108-
// return next();
109104
}
110105

111106
})

server/interfaces/interfaces.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

server/server.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
const app = require('./app');
2-
const colors = require('colors');
1+
const app = require("./app");
2+
const colors = require("colors");
33

44
const PORT = process.env.PORT || 3000;
55

6-
// Open up server on PORT
76
app.listen(PORT, () => {
87
console.log(`server is listening on port ${PORT}`.green.inverse);
98
});

0 commit comments

Comments
 (0)