Refactored endpoint

This commit is contained in:
Alex Holliday committed 2024-08-12 10:00:20 -07:00
1 parent 848ee60ca0
commit 6cbca39e0f
2 files changed
+15 -39

No files matched your search

+12 -35
View File
@@ -126,11 +126,12 @@ const loginController = async (req, res, next) => {
return; return;
} }
try { try {
const { email, password } = req.body;
// Check if user exists // Check if user exists
const user = await req.db.getUserByEmail(req, res); const user = await req.db.getUserByEmail(email);
// Compare password // Compare password
const match = await user.comparePassword(req.body.password); const match = await user.comparePassword(password);
if (match !== true) { if (match !== true) {
throw new Error(errorMessages.AUTH_INCORRECT_PASSWORD); throw new Error(errorMessages.AUTH_INCORRECT_PASSWORD);
} }
@@ -167,7 +168,7 @@ const logoutController = async (req, res, next) => {
try { try {
// Get user // Get user
const { email } = req.body; const { email } = req.body;
const userToLogout = await req.db.getUserByEmail({ body: { email } }, res); const userToLogout = await req.db.getUserByEmail(email);
userToLogout.authToken = null; userToLogout.authToken = null;
await userToLogout.save(); await userToLogout.save();
@@ -216,7 +217,7 @@ const userEditController = async (req, res, next) => {
// Add user email to body for DB operation // Add user email to body for DB operation
req.body.email = email; req.body.email = email;
// Get user // Get user
const user = await req.db.getUserByEmail(req, res); 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 403
@@ -350,7 +351,8 @@ const recoveryRequestController = async (req, res, next) => {
} }
try { try {
const user = await req.db.getUserByEmail(req, res); const { email } = req.body;
const user = await req.db.getUserByEmail(email);
if (user) { 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;
@@ -462,41 +464,16 @@ const deleteUserController = async (req, res, next) => {
const decodedToken = jwt.decode(token); const decodedToken = jwt.decode(token);
const { _id, email } = decodedToken; const { _id, email } = decodedToken;
// TODO fix this hack
const decodedTokenCastedAsRequest = {
query: {},
params: {
userId: _id,
},
body: {
email,
},
};
// Validate user
try {
await deleteUserParamValidation.validateAsync(
decodedTokenCastedAsRequest.body
);
} catch (error) {
error.status = 422;
error.service = SERVICE_NAME;
error.message =
error.details?.[0]?.message || error.message || "Validation Error";
next(error);
return;
}
// Check if the user exists // Check if the user exists
const user = await req.db.getUserByEmail(decodedTokenCastedAsRequest); const user = await req.db.getUserByEmail(email);
if (!user) { if (!user) {
throw new Error(errorMessages.DB_USER_NOT_FOUND); throw new Error(errorMessages.DB_USER_NOT_FOUND);
} }
// 1. Find all the monitors associated with the user id // 1. Find all the monitors associated with the user id
const monitors = await req.db.getMonitorsByUserId( const monitors = await req.db.getMonitorsByUserId({
decodedTokenCastedAsRequest params: { userId: _id },
); });
if (monitors) { if (monitors) {
//2. Remove all jobs, delete checks and alerts //2. Remove all jobs, delete checks and alerts
@@ -514,7 +491,7 @@ const deleteUserController = async (req, res, next) => {
await req.db.deleteMonitorsByUserId(user._id); await req.db.deleteMonitorsByUserId(user._id);
// 4. Delete the user by id // 4. Delete the user by id
await req.db.deleteUser(decodedTokenCastedAsRequest); await req.db.deleteUser(user._id);
return res.status(200).json({ return res.status(200).json({
success: true, success: true,
+3 -4
View File
@@ -51,11 +51,11 @@ const insertUser = async (req, res) => {
* @returns {Promise<UserModel>} * @returns {Promise<UserModel>}
* @throws {Error} * @throws {Error}
*/ */
const getUserByEmail = async (req, res) => { 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 returing the user
const user = await UserModel.findOne({ email: req.body.email }).select( const user = await UserModel.findOne({ email: email }).select(
"-profileImage" "-profileImage"
); );
if (user) { if (user) {
@@ -126,8 +126,7 @@ const updateUser = async (req, res) => {
* @returns {Promise<UserModel>} * @returns {Promise<UserModel>}
* @throws {Error} * @throws {Error}
*/ */
const deleteUser = async (req, res) => { const deleteUser = async (userId) => {
const userId = req.params.userId;
try { try {
const deletedUser = await UserModel.findByIdAndDelete(userId); const deletedUser = await UserModel.findByIdAndDelete(userId);
if (!deletedUser) { if (!deletedUser) {