diff --git a/server/controllers/otpController.js b/server/controllers/otpController.js index feac533..38ce36b 100644 --- a/server/controllers/otpController.js +++ b/server/controllers/otpController.js @@ -8,34 +8,28 @@ const { findUserByEmail, findUserById, } = require("../utils/PasswordTokenAndUser.js"); - const sendOTP = async (req, res) => { try { const { email } = req.body; if (!email) { return res.status(400).json({ - message: "You Haven't Entered the Email!", + message: "You haven't entered the email!", success: false, }); } - const studentExists = await User.findOne({ email }); - if (!studentExists) { return res.status(401).json({ success: false, message: "No user with the given email is registered!", }); } - let otp = otpGenerator.generate(6, { upperCaseAlphabets: false, lowerCaseAlphabets: false, specialChars: false, }); - let result = await OTP.findOne({ otp }); - while (result) { otp = otpGenerator.generate(6, { upperCaseAlphabets: false, @@ -44,40 +38,38 @@ const sendOTP = async (req, res) => { }); result = await OTP.findOne({ otp }); } - const otpSent = await OTP.create({ email, otp, }); - if (!otpSent) { - return res - .status(500) - .json({ message: "The Otp Was not Sent", success: false }); + return res.status(500).json({ + message: "The OTP was not sent", + success: false, + }); } - const info = await sendMail({ receiver: email, otp }); if (!info) { - console.log("Something went wrong while sending email"); + console.error("Something went wrong while sending email"); return res.status(500).json({ - message: "Something Went Wrong in mailing the person", + message: "Something went wrong in mailing the person", success: false, }); } - return res.status(200).json({ success: true, - message: "OTP Sent Successfully", + message: "OTP sent successfully", otp, }); } catch (err) { - console.log("Something went wrong while sending OTP", err); - return res - .status(500) - .json({ message: "Internal server error", success: false, err }); + console.error("Something went wrong while sending OTP", err); + return res.status(500).json({ + message: "Internal server error", + success: false, + err, + }); } }; - const verifyOTP = async (req, res) => { try { const { email, otp } = req.body; @@ -87,51 +79,37 @@ const verifyOTP = async (req, res) => { success: false, }); } - const otpRecord = await OTP.findOne({ email, otp }); if (!otpRecord) { return res.status(401).json({ success: false, - message: "Invalid OTP or Email!", + message: "Invalid OTP or email!", }); } - - // Optional: Check if OTP is expired (depending on your expiration logic) - // const isExpired = checkOtpExpiration(otpRecord); // Implement this function if needed - // if (isExpired) { - // return res.status(401).json({ - // success: false, - // message: "OTP is expired!", - // }); - // } - - // OTP is valid, perform necessary actions (e.g., mark user as verified) - - // Optionally delete the OTP record after verification await OTP.deleteOne({ email, otp }); const existingUser = await findUserByEmail(email); - if (existingUser) { const tokenReturn = forgotPasswordToken(existingUser); const link = `/api/v1/newPassword/${existingUser._id}/${tokenReturn}`; - console.log("Link is: ", link); + console.log("Link is:", link); return res.status(200).json({ success: true, - message: "OTP Verified Successfully", + message: "OTP verified successfully", link: link, }); } else { return res.status(401).json({ success: false, - message: "The Email cant be found in the database!", + message: "The email can't be found in the database!", }); } } catch (err) { - console.log("Something went wrong while verifying OTP ", err); - return res - .status(500) - .json({ message: "Internal server error", success: false, err }); + console.error("Something went wrong while verifying OTP", err); + return res.status(500).json({ + message: "Internal server error", + success: false, + err, + }); } }; - module.exports = { sendOTP, verifyOTP };