From 17d07f9d7dd3a2d9d2cbe1ca8a23e09823736770 Mon Sep 17 00:00:00 2001 From: phucd5 Date: Sat, 28 Oct 2023 16:52:18 -0400 Subject: [PATCH] fixed duped email and updated /search route --- server/controllers/auth.js | 2 +- server/controllers/user.js | 11 +++++------ server/routes/user.js | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/server/controllers/auth.js b/server/controllers/auth.js index 10bf081..6db4baa 100644 --- a/server/controllers/auth.js +++ b/server/controllers/auth.js @@ -18,7 +18,7 @@ export const register = async (req, res) => { userName, firstName, lastName, - email, + email: email.toLowerCase(), password: passwordHash, }); diff --git a/server/controllers/user.js b/server/controllers/user.js index 509b802..30b9a2e 100644 --- a/server/controllers/user.js +++ b/server/controllers/user.js @@ -24,14 +24,13 @@ export const getUserById = async (req, res) => { export const getUserByEmailOrUsername = async (req, res) => { try { - const { email, username } = req.body; - - const conditions = []; - if (email) conditions.push({ email }); - if (username) conditions.push({ userName: username }); + const { email, userName } = req.body; const user = await UserModel.findOne({ - $or: conditions, + $or: [ + { email: { $regex: new RegExp(`^${email}$`, "i") } }, + { userName: userName }, + ], }); if (!user) { diff --git a/server/routes/user.js b/server/routes/user.js index fbd9d56..ddaa8c5 100644 --- a/server/routes/user.js +++ b/server/routes/user.js @@ -13,7 +13,7 @@ import { const router = express.Router(); -router.post("/search", getUserByEmailOrUsername); +router.get("/search", getUserByEmailOrUsername); router.get("/:userId", getUserById); router.get("/:userId/friends", getUserFriends);