From 0e29d7fae717853da4f21f384c834583710ced3b Mon Sep 17 00:00:00 2001 From: Uttkarsh24 Date: Thu, 2 Jan 2025 13:07:28 +0530 Subject: [PATCH] Changes to forgotUsername route --- Backend/src/controllers/user.controller.js | 43 +++++++++++++++++++++- Backend/src/routes/user.routes.js | 2 + 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/Backend/src/controllers/user.controller.js b/Backend/src/controllers/user.controller.js index 3718f20..f475e0f 100644 --- a/Backend/src/controllers/user.controller.js +++ b/Backend/src/controllers/user.controller.js @@ -295,15 +295,55 @@ const verifyNewEmail = asyncHandler(async (req, res) => { } }); -const forgotUsername = asyncHandler(async (req, res) => { +const forgetUsernameVerificationEmail = asyncHandler(async (req, res) => { const { email } = req.body; + if (!email) { throw new ApiError(400, "All Fields Are Required."); } + const user = await User.findOne({ email: email }); + if (!user) { throw new ApiError(404, "Email Not Found."); } + + const verificationCode = (Math.floor(100000 + Math.random() * 900000)).toString(); + + try { + const updatedUser = await User.findByIdAndUpdate( + user._id, + { verificationCode: verificationCode }, + { new: true } + ); + + await sendVerificationCodeEmailQueue.add('verification-code-email', { userId: updatedUser._id }); + + return res + .status(201) + .json(new ApiResponse(201, null, "Mail Sent Successfully.")); + } catch (error) { + throw new ApiError(500, error.message || "Internal Server Error."); + } +}); + +const forgotUsername = asyncHandler(async (req, res) => { + const { email , code } = req.body; + + if (!email || !code) { + throw new ApiError(400, "All Fields Are Required."); + } + + const user = await User.findOne({ email: email }); + + if (!user) { + throw new ApiError(404, "Email Not Found."); + } + + if(user.verificationCode !== code) { + throw new ApiError(401, "Invalid Verification Code."); + } + try { await sendUsernameEmailQueue.add('username-email', { userId: user._id }); return res @@ -441,6 +481,7 @@ export { addDetails, changeEmail, verifyNewEmail, + forgetUsernameVerificationEmail, forgotUsername, forgotPasswordVerificationEmail, forgotPasswordVerificationCode, diff --git a/Backend/src/routes/user.routes.js b/Backend/src/routes/user.routes.js index b0477cd..309c286 100644 --- a/Backend/src/routes/user.routes.js +++ b/Backend/src/routes/user.routes.js @@ -9,6 +9,7 @@ import { addDetails, changeEmail, verifyNewEmail, + forgetUsernameVerificationEmail, forgotUsername, forgotPassword, forgotPasswordVerificationCode, @@ -37,6 +38,7 @@ const router = Router() router.route("/register").post(validationSchema(registerUserSchema), registerUser); router.route("/login").post(validationSchema(loginUserSchema), loginUser); router.route("/verifyCode").post(validationSchema(verifyCodeSchema), verifyCode); +router.route("/forgetUsernameVerificationEmail").post(validationSchema(forgotUsernameSchema), forgetUsernameVerificationEmail); router.route("/forgotUsername").post(validationSchema(forgotUsernameSchema), forgotUsername); router.route("/forgotPasswordVerificationEmail").post(validationSchema(forgotPasswordVerificationEmailSchema), forgotPasswordVerificationEmail); router.route("/forgotPasswordVerificationCode").post(validationSchema(forgotPasswordVerificationCodeSchema), forgotPasswordVerificationCode);