Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update otpController.js #483

Merged
merged 1 commit into from
Aug 5, 2024
Merged
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
70 changes: 24 additions & 46 deletions server/controllers/otpController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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 };
Loading