Skip to content

Commit 202e51b

Browse files
committed
new feature added
1 parent 8e16d26 commit 202e51b

File tree

9 files changed

+359
-86
lines changed

9 files changed

+359
-86
lines changed

Diff for: .envExample

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
PORT=7777
22
MONGO_DB_URL='mongodb://localhost:27017/Auth'
33
secretKey= '12345'
4-
expiresIn= '1hr'
4+
expiresIn= '1hr'
5+
OTPexpiresIn= '1min'
6+
7+
EMAIL_USER="example@gmail.com"
8+
EMAIL_PASS="PR0@@3956"

Diff for: config/db.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const connectDB = async () => {
55
await mongoose
66
.connect(process.env.MONGO_DB_URL)
77
.then(() => {
8-
console.log("MongoDB connected");
8+
console.log("Lets MongoDB");
99
})
1010
.catch((err) => console.error(err));
1111
};

Diff for: controllers/authController.js

+154-39
Original file line numberDiff line numberDiff line change
@@ -12,59 +12,54 @@ const generateToken = (user) => {
1212
{ expiresIn }
1313
);
1414
};
15+
const generateTempToken = (email) => {
16+
return jwt.sign({ email: email }, secretKey, {
17+
expiresIn: OTPexpiresIn,
18+
});
19+
};
20+
const sendOtp = async (email) => {
21+
try {
22+
generateTempToken(email);
23+
const otp = generateOTP();
24+
await sendVerificationEmail(email, otp);
1525

16-
// const register = async (req, res) => {
17-
// try {
18-
// const { username, email, password } = req.body;
19-
// const isUser = await User.findOne({ email });
20-
// if (!isUser) {
21-
// const hashedPassword = await bcrypt.hash(password, 10);
22-
// const user = await User.create({
23-
// username,
24-
// email,
25-
// password: hashedPassword,
26-
// });
27-
// const token = generateToken(user);
28-
// res.status(201).json({ user, token });
29-
// } else {
30-
// return res
31-
// .status(200)
32-
// .json({ message: "User already exist, try another email" });
33-
// }
34-
// } catch (error) {
35-
// res.status(400).json({ message: error.message });
36-
// }
37-
// };
38-
26+
const tempToken = generateTempToken(email);
27+
// console.log({ OTP: otp, Token: tempToken });
28+
// console.log("OTP sent successfully to:", email);
29+
return { tempToken, otp };
30+
} catch (error) {
31+
console.error("Failed to send OTP:", error);
32+
throw new Error("Failed to send OTP");
33+
}
34+
};
3935
const register = async (req, res) => {
4036
try {
4137
const { username, email, password } = req.body;
4238

4339
const existingUser = await User.findOne({ email });
4440
if (existingUser && existingUser.isVerified) {
45-
return res.status(400).json({ message: 'User already registered' });
41+
return res.status(400).json({
42+
message: "User already registered, Proceed to login or use new email",
43+
});
4644
}
4745
// console.log (existingUser);
4846

49-
const otp = generateOTP();
47+
const { tempToken, otp } = await sendOtp(email);
5048
// console.log (otp);
5149

5250
const hashedPassword = await bcrypt.hash(password, 10);
5351
const user = await User.findOneAndUpdate(
5452
{ email },
5553
{ username, password: hashedPassword, otp },
56-
{ new: true, upsert: true }
54+
{ new: true, upsert: true }
5755
);
5856
// console.log (user);
59-
60-
61-
// Send verification email with OTP
62-
await sendVerificationEmail(email, otp);
6357

64-
const tempToken = jwt.sign({ userId: email }, secretKey, {
65-
expiresIn: OTPexpiresIn,
58+
console.log({
59+
Message: "OTP sent Successfuly!",
60+
newTempToken: tempToken,
61+
OTP: otp,
6662
});
67-
console.log({ OTP: otp, Token: tempToken });
6863

6964
res.status(201).json({
7065
message: "Verify account using OTP sent to your email",
@@ -75,7 +70,66 @@ const register = async (req, res) => {
7570
res.status(500).json({ message: "Internal server error" });
7671
}
7772
};
73+
const verifyAccount = async (req, res) => {
74+
try {
75+
const { email, otp } = req.body;
76+
const user = await User.findOne({ email });
77+
if (!user) {
78+
return res.status(404).json({ Message: "user not found" });
79+
}
80+
if (user.isVerified) {
81+
return res
82+
.status(200)
83+
.json({ Message: "Your account is already varified" });
84+
}
85+
if (user.otp !== otp) {
86+
return res.status(400).json({ message: "Invalid OTP" });
87+
}
88+
89+
user.isVerified = true;
90+
user.otp = "";
91+
await user.save();
92+
93+
const token = generateToken(user);
94+
console.log("Logged In as_________________________" + user.username);
95+
return res.status(200).json([{ Status: "logged in" }, { user, token }]);
96+
} catch (error) {
97+
console.error(error);
98+
res.status(400).json({ message: error.message });
99+
}
100+
};
101+
const resendOtp = async (req, res) => {
102+
try {
103+
const { email } = req.body;
104+
const user = await User.findOne({ email });
105+
if (!user) {
106+
return res.status(404).json({ Message: "user not found" });
107+
}
108+
if (user.isVerified) {
109+
return res
110+
.status(200)
111+
.json({ Message: "Your account is already varified" });
112+
}
113+
114+
const { tempToken, otp } = await sendOtp(email);
115+
116+
user.otp = otp;
117+
await user.save();
118+
119+
console.log({
120+
Message: "OTP Resent Successfuly!",
121+
newTempToken: tempToken,
122+
OTP: otp,
123+
});
78124

125+
return res
126+
.status(200)
127+
.json({ Message: "OTP Resent Successfuly!", newTempToken: tempToken });
128+
} catch (error) {
129+
console.error(error);
130+
res.status(400).json({ message: error.message });
131+
}
132+
};
79133
const login = async (req, res) => {
80134
try {
81135
const { email, password } = req.body;
@@ -87,13 +141,21 @@ const login = async (req, res) => {
87141
if (!passwordMatch) {
88142
return res.status(401).json({ message: "Invalid password" });
89143
}
144+
if (!user.isVerified) {
145+
console.log(
146+
"TODO: redirect user to the OTP Verification page with email payload"
147+
);
148+
return res
149+
.status(403)
150+
.json("Your account is not verified, Please veify first !");
151+
}
90152
const token = generateToken(user);
153+
console.log("Logged In as_________________________" + user.username);
91154
res.json([{ Status: "logged in" }, { user, token }]);
92155
} catch (error) {
93156
res.status(400).json({ message: error.message });
94157
}
95158
};
96-
97159
const showInfo = async (req, res) => {
98160
try {
99161
// Access userId from req object
@@ -106,11 +168,6 @@ const showInfo = async (req, res) => {
106168
res.status(500).json({ message: error.message });
107169
}
108170
};
109-
110-
// const showInfo = async (req, res) => {
111-
// res.json("this is showInfo")
112-
// };
113-
114171
const changePassword = async (req, res) => {
115172
try {
116173
const { currentPassword, newPassword } = req.body;
@@ -140,10 +197,68 @@ const changePassword = async (req, res) => {
140197
res.status(500).json({ message: "Internal server error" });
141198
}
142199
};
200+
const forgrtPassword = async (req, res) => {
201+
try {
202+
const { email } = req.body;
203+
console.log(email);
204+
const user = await User.findOne({ email });
205+
console.log(user);
206+
if (!user) {
207+
return res.status(404).json({ Message: "user not found" });
208+
}
209+
210+
const { tempToken, otp } = await sendOtp(email);
211+
212+
user.otp = otp;
213+
await user.save();
214+
215+
console.log({
216+
Message: "OTP sent Successfuly!",
217+
tempToken: tempToken,
218+
OTP: otp,
219+
});
220+
221+
return res
222+
.status(200)
223+
.json({ Message: "OTP sent Successfuly!", tempToken: tempToken });
224+
} catch (error) {
225+
console.error(error);
226+
res.status(400).json({ message: error.message });
227+
}
228+
};
229+
const setNewPassword = async (req, res) => {
230+
//Receive new password from body or anywhere, see it while implemnting frontend
231+
// for now lets get new password through req.body
232+
const { user, newPassword } = req.body;
233+
234+
// TODO: Impliment params instead of req.body whenevr needed**** IMP
235+
// in verifyAccount, resendOtp
236+
237+
if (!user.isVerified) {
238+
console.log(
239+
"TODO: redirect user to the OTP Verification page with email payload"
240+
);
241+
return res
242+
.status(403)
243+
.json("Your account is not verified, Please veify first !");
244+
}
143245

246+
const hashedPassword = await bcrypt.hash(newPassword, 10);
247+
user.password = hashedPassword;
248+
user.tokenVersion += 1;
249+
await user.save();
250+
251+
// console.log("This is SetPassword()\n", user);
252+
// console.log("Extract:", user.password);
253+
res.status(200).json({ message: "Password changed successfully" });
254+
};
144255
module.exports = {
145256
register,
257+
verifyAccount,
258+
resendOtp,
146259
login,
147260
showInfo,
148261
changePassword,
262+
forgrtPassword,
263+
setNewPassword,
149264
};

Diff for: middlewares/accountVerify.js

Whitespace-only changes.

Diff for: middlewares/authMiddleware.js

-40
This file was deleted.

0 commit comments

Comments
 (0)