Skip to content

Commit d23855f

Browse files
committed
Add returns, fix error code
1 parent 5c92446 commit d23855f

10 files changed

Lines changed: 85 additions & 118 deletions

middlewares/account.middleware.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const Constants = {
2626
*/
2727
function parsePatch(req, res, next) {
2828
delete req.body.id;
29-
next();
29+
return next();
3030
}
3131

3232
/**
@@ -66,7 +66,7 @@ function parseAccount(req, res, next) {
6666

6767
req.body.accountDetails = accountDetails;
6868

69-
next();
69+
return next();
7070
}
7171

7272
/**
@@ -77,13 +77,13 @@ function parseAccount(req, res, next) {
7777
*/
7878
async function updatePassword(req, res, next) {
7979
req.body.account = await Services.Account.updatePassword(req.body.decodedToken.accountId, req.body.password);
80-
next();
80+
return next();
8181
}
8282

8383
// TODO: fix when new permission system is created
8484
async function addDefaultHackerPermissions(req, res, next) {
8585
// await Services.RoleBinding.createRoleBinding(req.);
86-
next();
86+
return next();
8787
}
8888

8989
/**
@@ -100,8 +100,8 @@ async function addAccount(req, res, next) {
100100
//Check duplicate
101101
const exists = await Services.Account.findByEmail(accountDetails.email);
102102
if (exists) {
103-
next({
104-
status: 500,
103+
return next({
104+
status: 422,
105105
message: Constants.Error.ACCOUNT_DUPLICATE_422_MESSAGE,
106106
error: {
107107
route: req.path
@@ -110,7 +110,7 @@ async function addAccount(req, res, next) {
110110
}
111111
const account = await Services.Account.addOneAccount(accountDetails);
112112
req.body.account = account;
113-
next();
113+
return next();
114114
}
115115

116116
/**
@@ -122,9 +122,9 @@ async function addAccount(req, res, next) {
122122
async function updateAccount(req, res, next) {
123123
const account = await Services.Account.updateOne(req.params.id, req.body);
124124
if (account) {
125-
next();
125+
return next();
126126
} else {
127-
next({
127+
return next({
128128
status: 404,
129129
message: Constants.Error.ACCOUNT_404_MESSAGE,
130130
data: {

middlewares/auth.middleware.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function login(req, res, next) {
5353
error: {}
5454
});
5555
}
56-
next();
56+
return next();
5757
});
5858
})(req, res, next);
5959
}
@@ -65,15 +65,15 @@ function login(req, res, next) {
6565
function ensureAuthenticated() {
6666
return function (req, res, next) {
6767
if (req.isUnauthenticated()) {
68-
next({
68+
return next({
6969
status: 401,
7070
message: Constants.Error.AUTH_401_MESSAGE,
7171
error: {
7272
route: req.path
7373
}
7474
});
7575
} else {
76-
next();
76+
return next();
7777
}
7878
};
7979
}
@@ -88,19 +88,19 @@ function ensureAuthorized(findByIdFns) {
8888
Services.Auth.ensureAuthorized(req, findByIdFns).then(
8989
(auth) => {
9090
if (!auth) {
91-
next({
91+
return next({
9292
status: 403,
9393
message: Constants.Error.AUTH_403_MESSAGE,
9494
error: {
9595
route: req.path
9696
}
9797
});
9898
} else {
99-
next();
99+
return next();
100100
}
101101
},
102102
(err) => {
103-
next(err);
103+
return next(err);
104104
}
105105
);
106106
};
@@ -121,7 +121,7 @@ async function retrieveRoleBindings(req, res, next) {
121121
})
122122
}
123123
req.roleBindings = roleBindings;
124-
next();
124+
return next();
125125
}
126126

127127
/**
@@ -145,9 +145,9 @@ async function sendResetPasswordEmailMiddleware(req, res, next) {
145145
if (mailData !== undefined) {
146146
Services.Email.send(mailData, (err) => {
147147
if (err) {
148-
next(err);
148+
return next(err);
149149
} else {
150-
next();
150+
return next();
151151
}
152152
});
153153
} else {
@@ -157,7 +157,7 @@ async function sendResetPasswordEmailMiddleware(req, res, next) {
157157
}
158158
} else {
159159
//Didn't find the user, but we don't want to throw an error because someone might be trying to see who has an account.
160-
next();
160+
return next();
161161
}
162162
}
163163

@@ -178,9 +178,9 @@ async function sendConfirmAccountEmailMiddleware(req, res, next) {
178178
if (mailData !== undefined) {
179179
Services.Email.send(mailData, (err) => {
180180
if (err) {
181-
next(err);
181+
return next(err);
182182
} else {
183-
next();
183+
return next();
184184
}
185185
});
186186
} else {
@@ -216,9 +216,9 @@ async function resendConfirmAccountEmail(req, res, next) {
216216
if (mailData !== undefined) {
217217
Services.Email.send(mailData, (err) => {
218218
if (err) {
219-
next(err);
219+
return next(err);
220220
} else {
221-
next();
221+
return next();
222222
}
223223
});
224224
} else {
@@ -238,10 +238,10 @@ async function resendConfirmAccountEmail(req, res, next) {
238238
function parseResetToken(req, res, next) {
239239
jwt.verify(req.body['x-reset-token'], process.env.JWT_RESET_PWD_SECRET, function (err, decoded) {
240240
if (err) {
241-
next(err);
241+
return next(err);
242242
} else {
243243
req.body.decodedToken = decoded;
244-
next();
244+
return next();
245245
}
246246
});
247247
}
@@ -258,13 +258,13 @@ function parseAccountConfirmationToken(req, res, next) {
258258
if (!!req.body.token) {
259259
jwt.verify(req.body.token, process.env.JWT_CONFIRM_ACC_SECRET, function (err, decoded) {
260260
if (err) {
261-
next(err);
261+
return next(err);
262262
} else {
263263
req.body.decodedToken = decoded;
264264
}
265265
});
266266
}
267-
next();
267+
return next();
268268
}
269269

270270
/**
@@ -277,10 +277,10 @@ async function getAccountTypeFromConfirmationToken(req, res, next) {
277277
const confirmationObj = await Services.AccountConfirmation.findById(req.body.decodedToken.accountConfirmationId);
278278
if (confirmationObj) {
279279
req.body.accountType = confirmationObj.accountType;
280-
next();
280+
return next();
281281
} else {
282282
//Either the token was already used, it's invalid, or user does not exist.
283-
next({
283+
return next({
284284
status: 401,
285285
message: Constants.Error.ACCOUNT_TOKEN_401_MESSAGE,
286286
error: {}
@@ -299,10 +299,10 @@ async function validateResetToken(req, res, next) {
299299
const userObj = await Services.Account.findById(req.body.decodedToken.accountId);
300300
if (resetObj && userObj) {
301301
req.body.user = userObj;
302-
next();
302+
return next();
303303
} else {
304304
//Either the token was already used, it's invalid, or user does not exist.
305-
next({
305+
return next({
306306
status: 401,
307307
message: Constants.Error.ACCOUNT_TOKEN_401_MESSAGE,
308308
error: {}
@@ -324,10 +324,10 @@ async function validateConfirmationToken(req, res, next) {
324324
userObj.accountType = confirmationObj.accountType;
325325
await Services.Account.updateOne(confirmationObj.accountId, userObj);
326326
req.body.user = userObj;
327-
next();
327+
return next();
328328
} else {
329329
//Either the token was already used, it's invalid, or user does not exist.
330-
next({
330+
return next({
331331
status: 401,
332332
message: Constants.Error.ACCOUNT_TOKEN_401_MESSAGE,
333333
error: {}
@@ -349,7 +349,7 @@ async function validateConfirmationTokenWithoutAccount(req, res, next) {
349349
req.body.accountDetails.accountType = confirmationObj.accountType;
350350
}
351351
}
352-
next();
352+
return next();
353353
}
354354

355355

@@ -362,10 +362,10 @@ async function validateConfirmationTokenWithoutAccount(req, res, next) {
362362
function deleteResetToken(req, res, next) {
363363
Services.ResetPasswordToken.deleteToken(req.body.decodedToken.resetId).then(
364364
() => {
365-
next();
365+
return next();
366366
},
367367
(err) => {
368-
next(err);
368+
return next(err);
369369
}
370370
);
371371
}
@@ -380,7 +380,7 @@ async function addCreationRoleBindings(req, res, next) {
380380
// Get the default role for the account type given
381381
const roleName = Constants.General.POST_ROLES[req.body.account.accountType];
382382
await Services.RoleBinding.createRoleBindingByRoleName(req.body.account.id, roleName);
383-
next();
383+
return next();
384384
}
385385

386386
/**
@@ -390,7 +390,7 @@ async function addCreationRoleBindings(req, res, next) {
390390
function createRoleBindings(roleName = undefined) {
391391
return async (req, res, next) => {
392392
await Services.RoleBinding.createRoleBindingByRoleName(req.user.id, roleName);
393-
next();
393+
return next();
394394
}
395395
}
396396

@@ -403,7 +403,7 @@ function createRoleBindings(roleName = undefined) {
403403
async function addSponsorRoleBindings(req, res, next) {
404404
const account = Services.Account.findById(req.body.sponsorDetails.accountId);
405405
await Services.RoleBinding.createRoleBindingByRoleName(account.id, account.accountType);
406-
next();
406+
return next();
407407
}
408408

409409
module.exports = {

0 commit comments

Comments
 (0)