Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions api/src/api/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ router.put(
);

await Resource.updateMany(
{ category: req.body.category, subcategory: req.body.currentName },
{ $set: { 'subcategory.$': req.body.newName } },
{ category: req.body.category, subcategories: req.body.currentName },
{ $set: { 'subcategories.$': req.body.newName } },
);

res.json({
Expand Down Expand Up @@ -289,8 +289,8 @@ router.delete(
);

await Resource.updateMany(
{ category: req.body.category, subcategory: req.body.subcategory },
{ $pull: { subcategory: req.body.subcategory } },
{ category: req.body.category, subcategories: req.body.subcategory },
{ $pull: { subcategories: req.body.subcategory } },
);

res.json({
Expand Down
2 changes: 1 addition & 1 deletion api/src/models/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const HoursSchema = new mongoose.Schema({

const Resource = new mongoose.Schema({
category: { type: [String], required: true },
subcategory: { type: [String], required: true },
subcategories: { type: [String], required: true },
name: { type: String, required: true },
description: { type: String, required: true },
website: { type: String, required: false },
Expand Down
14 changes: 5 additions & 9 deletions auth/src/api/changePassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,15 @@ const { googleAuth } = require("../utils/getConfigFile");
router.post(
"/changePassword",
[
check("currentPassword")
.isString()
.isLength({ min: 1 }),
check("newPassword")
.isString()
.isLength({ min: 1 })
check("currentPassword").isString().isLength({ min: 1 }),
check("newPassword").isString().isLength({ min: 1 }),
],
handleAsyncErrors(async function(req, res) {
handleAsyncErrors(async function (req, res) {
// Checks that the token is in the header and the currentPassword and newPassword are in the body of the request
const errors = validationResult(req);
if (!errors.isEmpty()) {
return sendResponse(res, 400, "Invalid request", {
errors: errors.array({ onlyFirstError: true })
errors: errors.array({ onlyFirstError: true }),
});
}
if (!req.headers.token) {
Expand Down Expand Up @@ -56,7 +52,7 @@ router.post(
}
// Sends a success message along with the new token.
sendResponse(res, 200, "Successful change of password!", {
token: new_token
token: new_token,
});
}
} else {
Expand Down
13 changes: 4 additions & 9 deletions auth/src/api/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,13 @@ const handleAsyncErrors = require("../utils/errorHandler");

router.post(
"/login",
[
check("email").isEmail(),
check("password")
.isString()
.isLength({ min: 1 })
],
handleAsyncErrors(async function(req, res) {
[check("email").isEmail(), check("password").isString().isLength({ min: 1 })],
handleAsyncErrors(async function (req, res) {
// Checks that the request has the required fields (email, password)
const errors = validationResult(req);
if (!errors.isEmpty()) {
return sendResponse(res, 400, "Invalid Request", {
errors: errors.array({ onlyFirstError: true })
errors: errors.array({ onlyFirstError: true }),
});
}

Expand All @@ -37,7 +32,7 @@ router.post(
message: "Successful login!",
token: jwt_token,
uid: user._id,
permission: user.role
permission: user.role,
});
} else {
return sendResponse(res, 400, "Password incorrect. Please try again.");
Expand Down
21 changes: 7 additions & 14 deletions auth/src/api/passwordReset.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { sendPasswordChangeEmail } = require("../utils/sendMail");
const { signAuthJWT } = require("../utils/jwtHelpers");
const {
isGmailEnabledForForgotPassword,
isSecurityQuestionEnabled
isSecurityQuestionEnabled,
} = require("../utils/getConfigFile");
const { expirePIN } = require("../utils/pinHelpers");
const handleAsyncErrors = require("../utils/errorHandler");
Expand All @@ -16,23 +16,16 @@ router.post(
"/passwordReset",
[
check("email").isEmail(),
check("password")
.isString()
.isLength({ min: 1 }),
check("pin")
.isNumeric()
.optional(),
check("answer")
.isString()
.isLength({ min: 1 })
.optional()
check("password").isString().isLength({ min: 1 }),
check("pin").isNumeric().optional(),
check("answer").isString().isLength({ min: 1 }).optional(),
],
handleAsyncErrors(async function(req, res) {
handleAsyncErrors(async function (req, res) {
// Checks that the email, password, and pin or answer (depending on the config file) is in the body of the request
const errors = validationResult(req);
if (!errors.isEmpty()) {
return sendResponse(res, 400, "Invalid request", {
errors: errors.array({ onlyFirstError: true })
errors: errors.array({ onlyFirstError: true }),
});
}

Expand Down Expand Up @@ -103,7 +96,7 @@ router.post(
// Responds to the request with a success message and a JWT token
sendResponse(res, 200, "Password successfully reset", {
token: await signAuthJWT(user._id, user.password),
permission: user.role
permission: user.role,
});
})
);
Expand Down
22 changes: 9 additions & 13 deletions auth/src/api/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ const { sendResponse } = require("./../utils/sendResponse");
const {
getRolesForUser,
getSuperiorsForRole,
getSecurityQuestions
getSecurityQuestions,
} = require("./../utils/getConfigFile");
const { signAuthJWT } = require("../utils/jwtHelpers");
const { generatePIN } = require("../utils/pinHelpers");
const {
googleAuth,
isSecurityQuestionEnabled
isSecurityQuestionEnabled,
} = require("../utils/getConfigFile");
const { sendMail } = require("./../utils/sendMail");
const handleAsyncErrors = require("../utils/errorHandler");
Expand All @@ -21,19 +21,15 @@ router.post(
"/register",
[
check("email").isEmail(),
check("password")
.isString()
.isLength({ min: 1 }),
check("role")
.isString()
.isLength({ min: 1 })
check("password").isString().isLength({ min: 1 }),
check("role").isString().isLength({ min: 1 }),
],
handleAsyncErrors(async function(req, res) {
handleAsyncErrors(async function (req, res) {
// Checks that the request has the required fields (email, password, and role)
const errors = validationResult(req);
if (!errors.isEmpty()) {
return sendResponse(res, 400, "Invalid Request", {
errors: errors.array({ onlyFirstError: true })
errors: errors.array({ onlyFirstError: true }),
});
}

Expand All @@ -49,7 +45,7 @@ router.post(
password: encodedPassword,
role: req.body.role,
verified: false,
savedResources: []
savedResources: [],
};

// If the security question is enabled, checks that the security question index is valid and that there is an answer
Expand Down Expand Up @@ -89,7 +85,7 @@ router.post(
subject: "New User Verification",
text:
"Thanks for signing up! Please enter the following PIN on the new user confirmation page: " +
user.pin
user.pin,
};
try {
await sendMail(body);
Expand All @@ -111,7 +107,7 @@ router.post(
message: "User added successfully!",
token: jwt_token,
uid: user._id,
permission: user.role
permission: user.role,
});
})
);
Expand Down
8 changes: 3 additions & 5 deletions auth/src/api/rolesChange.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ router.post(
"/roleschange",
[
check("userEmail").isEmail(),
check("newRole")
.isString()
.isLength({ min: 1 })
check("newRole").isString().isLength({ min: 1 }),
],
handleAsyncErrors(async function(req, res) {
handleAsyncErrors(async function (req, res) {
// Check that it has the email and new role of the user being promoted
const errors = validationResult(req);
if (!errors.isEmpty()) {
return sendResponse(res, 400, "Invalid request", {
errors: errors.array({ onlyFirstError: true })
errors: errors.array({ onlyFirstError: true }),
});
}

Expand Down
14 changes: 5 additions & 9 deletions auth/src/api/updateSecurityQuestion.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,15 @@ router.post(
"/updateSecurityQuestion",
[
check("questionIdx").isNumeric(),
check("answer")
.isString()
.isLength({ min: 1 }),
check("password")
.isString()
.isLength({ min: 1 })
check("answer").isString().isLength({ min: 1 }),
check("password").isString().isLength({ min: 1 }),
],
handleAsyncErrors(async function(req, res) {
handleAsyncErrors(async function (req, res) {
// Checks that the token is in the header and the questionIdx, answer, and passwword are in the body of the request.
const errors = validationResult(req);
if (!errors.isEmpty()) {
return sendResponse(res, 400, "Invalid request", {
errors: errors.array({ onlyFirstError: true })
errors: errors.array({ onlyFirstError: true }),
});
}
if (!req.headers.token) {
Expand Down Expand Up @@ -69,7 +65,7 @@ router.post(
{ _id: user._id },
{
question: question,
answer: req.body.answer.toLowerCase().replace(/\s/g, "")
answer: req.body.answer.toLowerCase().replace(/\s/g, ""),
}
);
return sendResponse(res, 200, "Succesfully added the security question");
Expand Down