Removed profile image from user before returning it

This commit is contained in:
Alex Holliday committed 2024-07-04 12:00:37 -07:00
1 parent 6509043b1f
commit f97382c88d
1 file changed
+12 -4
+12 -4
View File
@@ -42,7 +42,9 @@ const insertUser = async (req, res) => {
} }
const newUser = new UserModel(userData); const newUser = new UserModel(userData);
await newUser.save(); await newUser.save();
return await UserModel.findOne({ _id: newUser._id }).select("-password"); // .select() doesn't work with create, need to save then find return await UserModel.findOne({ _id: newUser._id })
.select("-password")
.select("-profileImage"); // .select() doesn't work with create, need to save then find
} catch (error) { } catch (error) {
if (error.code === DUPLICATE_KEY_CODE) { if (error.code === DUPLICATE_KEY_CODE) {
throw new Error(errorMessages.DB_USER_EXISTS); throw new Error(errorMessages.DB_USER_EXISTS);
@@ -66,7 +68,9 @@ const getUserByEmail = async (req, res) => {
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 }); const user = await UserModel.findOne(
{ email: req.body.email }.select("-profileImage")
);
if (user) { if (user) {
return user; return user;
} else { } else {
@@ -102,7 +106,9 @@ const updateUser = async (req, res) => {
candidateUserId, candidateUserId,
candidateUser, candidateUser,
{ new: true } // Returns updated user instead of pre-update user { new: true } // Returns updated user instead of pre-update user
).select("-password"); )
.select("-password")
.select("-profileImage");
return updatedUser; return updatedUser;
} catch (error) { } catch (error) {
throw error; throw error;
@@ -168,7 +174,9 @@ const resetPassword = async (req, res) => {
// Fetch the user again without the password // Fetch the user again without the password
const userWithoutPassword = await UserModel.findOne({ const userWithoutPassword = await UserModel.findOne({
email: recoveryToken.email, email: recoveryToken.email,
}).select("-password"); })
.select("-password")
.select("-profileImage");
return userWithoutPassword; return userWithoutPassword;
} else { } else {
throw new Error(errorMessages.DB_USER_NOT_FOUND); throw new Error(errorMessages.DB_USER_NOT_FOUND);