Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion Backend/src/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -441,6 +481,7 @@ export {
addDetails,
changeEmail,
verifyNewEmail,
forgetUsernameVerificationEmail,
forgotUsername,
forgotPasswordVerificationEmail,
forgotPasswordVerificationCode,
Expand Down
2 changes: 2 additions & 0 deletions Backend/src/routes/user.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
addDetails,
changeEmail,
verifyNewEmail,
forgetUsernameVerificationEmail,
forgotUsername,
forgotPassword,
forgotPasswordVerificationCode,
Expand Down Expand Up @@ -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);
Expand Down