Merge pull request #1054 from ajhollid/fix/be/refactor-auth-controller
Fix/be/refactor auth controller
This commit is contained in:
3 files changed
+24
-36
No files matched your search
@@ -279,10 +279,10 @@ const editUser = async (req, res, next) => {
|
|||||||
const user = await req.db.getUserByEmail(email);
|
const user = await req.db.getUserByEmail(email);
|
||||||
// Compare passwords
|
// Compare passwords
|
||||||
const match = await user.comparePassword(req.body.password);
|
const match = await user.comparePassword(req.body.password);
|
||||||
// If not a match, throw a 403
|
// If not a match, throw a 401
|
||||||
if (!match) {
|
if (!match) {
|
||||||
const error = new Error(errorMessages.AUTH_INCORRECT_PASSWORD);
|
const error = new Error(errorMessages.AUTH_INCORRECT_PASSWORD);
|
||||||
error.status = 403;
|
error.status = 401;
|
||||||
next(error);
|
next(error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -346,32 +346,26 @@ const requestRecovery = async (req, res, next) => {
|
|||||||
try {
|
try {
|
||||||
const { email } = req.body;
|
const { email } = req.body;
|
||||||
const user = await req.db.getUserByEmail(email);
|
const user = await req.db.getUserByEmail(email);
|
||||||
if (user) {
|
const recoveryToken = await req.db.requestRecoveryToken(req, res);
|
||||||
const recoveryToken = await req.db.requestRecoveryToken(req, res);
|
const name = user.firstName;
|
||||||
const name = user.firstName;
|
const { clientHost } = req.settingsService.getSettings();
|
||||||
const email = req.body.email;
|
const url = `${clientHost}/set-new-password/${recoveryToken.token}`;
|
||||||
const { clientHost } = req.settingsService.getSettings();
|
const msgId = await req.emailService.buildAndSendEmail(
|
||||||
const url = `${clientHost}/set-new-password/${recoveryToken.token}`;
|
"passwordResetTemplate",
|
||||||
|
{
|
||||||
const msgId = await req.emailService.buildAndSendEmail(
|
name,
|
||||||
"passwordResetTemplate",
|
|
||||||
{
|
|
||||||
name,
|
|
||||||
email,
|
|
||||||
url,
|
|
||||||
},
|
|
||||||
email,
|
email,
|
||||||
"Bluewave Uptime Password Reset"
|
url,
|
||||||
);
|
},
|
||||||
|
email,
|
||||||
|
"Bluewave Uptime Password Reset"
|
||||||
|
);
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.AUTH_CREATE_RECOVERY_TOKEN,
|
msg: successMessages.AUTH_CREATE_RECOVERY_TOKEN,
|
||||||
data: msgId,
|
data: msgId,
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
throw new Error(errorMessages.FRIENDLY_ERROR);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleError(error, SERVICE_NAME, "recoveryRequestController"));
|
next(handleError(error, SERVICE_NAME, "recoveryRequestController"));
|
||||||
}
|
}
|
||||||
@@ -430,7 +424,6 @@ const resetPassword = async (req, res, next) => {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const user = await req.db.resetPassword(req, res);
|
const user = await req.db.resetPassword(req, res);
|
||||||
|
|
||||||
const appSettings = await req.settingsService.getSettings();
|
const appSettings = await req.settingsService.getSettings();
|
||||||
const token = issueToken(user._doc, tokenType.ACCESS_TOKEN, appSettings);
|
const token = issueToken(user._doc, tokenType.ACCESS_TOKEN, appSettings);
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
@@ -460,11 +453,6 @@ const deleteUser = async (req, res, next) => {
|
|||||||
|
|
||||||
// Check if the user exists
|
// Check if the user exists
|
||||||
const user = await req.db.getUserByEmail(email);
|
const user = await req.db.getUserByEmail(email);
|
||||||
if (!user) {
|
|
||||||
next(new Error(errorMessages.DB_USER_NOT_FOUND));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 1. Find all the monitors associated with the team ID if superadmin
|
// 1. Find all the monitors associated with the team ID if superadmin
|
||||||
|
|
||||||
const result = await req.db.getMonitorsByTeamId({
|
const result = await req.db.getMonitorsByTeamId({
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ const insertUser = async (userData, imageFile) => {
|
|||||||
const getUserByEmail = async (email) => {
|
const getUserByEmail = async (email) => {
|
||||||
try {
|
try {
|
||||||
// Need the password to be able to compare, removed .select()
|
// Need the password to be able to compare, removed .select()
|
||||||
// We can strip the hash before returing the user
|
// We can strip the hash before returning the user
|
||||||
const user = await UserModel.findOne({ email: email }).select("-profileImage");
|
const user = await UserModel.findOne({ email: email }).select("-profileImage");
|
||||||
if (user) {
|
if (user) {
|
||||||
return user;
|
return user;
|
||||||
|
|||||||
@@ -467,7 +467,7 @@ describe("Auth Controller - editUser", async () => {
|
|||||||
});
|
});
|
||||||
await editUser(req, res, next);
|
await editUser(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].status).to.equal(403);
|
expect(next.firstCall.args[0].status).to.equal(401);
|
||||||
expect(next.firstCall.args[0].message).to.equal(
|
expect(next.firstCall.args[0].message).to.equal(
|
||||||
errorMessages.AUTH_INCORRECT_PASSWORD
|
errorMessages.AUTH_INCORRECT_PASSWORD
|
||||||
);
|
);
|
||||||
@@ -812,9 +812,9 @@ describe("Auth Controller - deleteUser", () => {
|
|||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
sinon.restore();
|
sinon.restore();
|
||||||
});
|
});
|
||||||
it("should return 404 if user is not found", async () => {
|
it("should throw an error if user is not found", async () => {
|
||||||
jwt.decode.returns({ email: "test@example.com" });
|
jwt.decode.returns({ email: "test@example.com" });
|
||||||
req.db.getUserByEmail.resolves(null);
|
req.db.getUserByEmail.throws(new Error(errorMessages.DB_USER_NOT_FOUND));
|
||||||
|
|
||||||
await deleteUser(req, res, next);
|
await deleteUser(req, res, next);
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user