Skip to content

Commit 154ca92

Browse files
Update otpController.js
Proper logging for debugging. Consistent error messages. Handling edge cases more effectively.
1 parent 61d4381 commit 154ca92

File tree

1 file changed

+24
-46
lines changed

1 file changed

+24
-46
lines changed

server/controllers/otpController.js

+24-46
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,28 @@ const {
88
findUserByEmail,
99
findUserById,
1010
} = require("../utils/PasswordTokenAndUser.js");
11-
1211
const sendOTP = async (req, res) => {
1312
try {
1413
const { email } = req.body;
1514
if (!email) {
1615
return res.status(400).json({
17-
message: "You Haven't Entered the Email!",
16+
message: "You haven't entered the email!",
1817
success: false,
1918
});
2019
}
21-
2220
const studentExists = await User.findOne({ email });
23-
2421
if (!studentExists) {
2522
return res.status(401).json({
2623
success: false,
2724
message: "No user with the given email is registered!",
2825
});
2926
}
30-
3127
let otp = otpGenerator.generate(6, {
3228
upperCaseAlphabets: false,
3329
lowerCaseAlphabets: false,
3430
specialChars: false,
3531
});
36-
3732
let result = await OTP.findOne({ otp });
38-
3933
while (result) {
4034
otp = otpGenerator.generate(6, {
4135
upperCaseAlphabets: false,
@@ -44,40 +38,38 @@ const sendOTP = async (req, res) => {
4438
});
4539
result = await OTP.findOne({ otp });
4640
}
47-
4841
const otpSent = await OTP.create({
4942
email,
5043
otp,
5144
});
52-
5345
if (!otpSent) {
54-
return res
55-
.status(500)
56-
.json({ message: "The Otp Was not Sent", success: false });
46+
return res.status(500).json({
47+
message: "The OTP was not sent",
48+
success: false,
49+
});
5750
}
58-
5951
const info = await sendMail({ receiver: email, otp });
6052
if (!info) {
61-
console.log("Something went wrong while sending email");
53+
console.error("Something went wrong while sending email");
6254
return res.status(500).json({
63-
message: "Something Went Wrong in mailing the person",
55+
message: "Something went wrong in mailing the person",
6456
success: false,
6557
});
6658
}
67-
6859
return res.status(200).json({
6960
success: true,
70-
message: "OTP Sent Successfully",
61+
message: "OTP sent successfully",
7162
otp,
7263
});
7364
} catch (err) {
74-
console.log("Something went wrong while sending OTP", err);
75-
return res
76-
.status(500)
77-
.json({ message: "Internal server error", success: false, err });
65+
console.error("Something went wrong while sending OTP", err);
66+
return res.status(500).json({
67+
message: "Internal server error",
68+
success: false,
69+
err,
70+
});
7871
}
7972
};
80-
8173
const verifyOTP = async (req, res) => {
8274
try {
8375
const { email, otp } = req.body;
@@ -87,51 +79,37 @@ const verifyOTP = async (req, res) => {
8779
success: false,
8880
});
8981
}
90-
9182
const otpRecord = await OTP.findOne({ email, otp });
9283
if (!otpRecord) {
9384
return res.status(401).json({
9485
success: false,
95-
message: "Invalid OTP or Email!",
86+
message: "Invalid OTP or email!",
9687
});
9788
}
98-
99-
// Optional: Check if OTP is expired (depending on your expiration logic)
100-
// const isExpired = checkOtpExpiration(otpRecord); // Implement this function if needed
101-
// if (isExpired) {
102-
// return res.status(401).json({
103-
// success: false,
104-
// message: "OTP is expired!",
105-
// });
106-
// }
107-
108-
// OTP is valid, perform necessary actions (e.g., mark user as verified)
109-
110-
// Optionally delete the OTP record after verification
11189
await OTP.deleteOne({ email, otp });
11290
const existingUser = await findUserByEmail(email);
113-
11491
if (existingUser) {
11592
const tokenReturn = forgotPasswordToken(existingUser);
11693
const link = `/api/v1/newPassword/${existingUser._id}/${tokenReturn}`;
117-
console.log("Link is: ", link);
94+
console.log("Link is:", link);
11895
return res.status(200).json({
11996
success: true,
120-
message: "OTP Verified Successfully",
97+
message: "OTP verified successfully",
12198
link: link,
12299
});
123100
} else {
124101
return res.status(401).json({
125102
success: false,
126-
message: "The Email cant be found in the database!",
103+
message: "The email can't be found in the database!",
127104
});
128105
}
129106
} catch (err) {
130-
console.log("Something went wrong while verifying OTP ", err);
131-
return res
132-
.status(500)
133-
.json({ message: "Internal server error", success: false, err });
107+
console.error("Something went wrong while verifying OTP", err);
108+
return res.status(500).json({
109+
message: "Internal server error",
110+
success: false,
111+
err,
112+
});
134113
}
135114
};
136-
137115
module.exports = { sendOTP, verifyOTP };

0 commit comments

Comments
 (0)