Merge branch 'master' into feat/server-error-msgs

This commit is contained in:
Alex Holliday
2024-06-12 20:03:30 -07:00
10 changed files with 102 additions and 118 deletions
-2
View File
@@ -1,4 +1,3 @@
const express = require("express");
const logger = require("../utils/logger");
require("dotenv").config();
const {
@@ -11,7 +10,6 @@ const {
editAlertBodyValidation,
deleteAlertParamValidation,
} = require("../validation/joi");
const { get } = require("mongoose");
const SERVICE_NAME = "alerts";
+2 -14
View File
@@ -45,18 +45,6 @@ const registerController = async (req, res, next) => {
return;
}
// Check if the user exists
try {
const isUser = await req.db.getUserByEmail(req, res);
if (isUser) {
throw new Error("User already exists");
}
} catch (error) {
error.service = SERVICE_NAME;
next(error);
return;
}
// Create a new user
try {
const newUser = await req.db.insertUser(req, res);
@@ -101,8 +89,8 @@ const loginController = async (req, res, next) => {
// Compare password
const match = await user.comparePassword(req.body.password);
if (!match) {
throw new Error("Password does not match!");
if (match !== true) {
throw new Error("Incorrect password");
}
// Remove password from user object. Should this be abstracted to DB layer?
+4
View File
@@ -5,6 +5,7 @@ const Check = require("../models/Check");
const Alert = require("../models/Alert");
const RecoveryToken = require("../models/RecoveryToken");
const crypto = require("crypto");
const DUPLICATE_KEY_CODE = 11000; // MongoDB error code for duplicate key
const connect = async () => {
try {
@@ -34,6 +35,9 @@ const insertUser = async (req, res) => {
await newUser.save();
return await UserModel.findOne({ _id: newUser._id }).select("-password"); // .select() doesn't work with create, need to save then find
} catch (error) {
if (error.code === DUPLICATE_KEY_CODE) {
throw new Error("Email already exists");
}
throw error;
}
};
-1
View File
@@ -4,7 +4,6 @@ const handleErrors = (error, req, res, next) => {
const status = error.status || 500;
const message = error.message || "Something went wrong";
const service = error.service || "Unknown service";
logger.error(error.message, { service: service });
res.status(status).json({ success: false, msg: message });
};