@@ -12,59 +12,54 @@ const generateToken = (user) => {
12
12
{ expiresIn }
13
13
) ;
14
14
} ;
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 ) ;
15
25
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
+ } ;
39
35
const register = async ( req , res ) => {
40
36
try {
41
37
const { username, email, password } = req . body ;
42
38
43
39
const existingUser = await User . findOne ( { email } ) ;
44
40
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
+ } ) ;
46
44
}
47
45
// console.log (existingUser);
48
46
49
- const otp = generateOTP ( ) ;
47
+ const { tempToken , otp } = await sendOtp ( email ) ;
50
48
// console.log (otp);
51
49
52
50
const hashedPassword = await bcrypt . hash ( password , 10 ) ;
53
51
const user = await User . findOneAndUpdate (
54
52
{ email } ,
55
53
{ username, password : hashedPassword , otp } ,
56
- { new : true , upsert : true }
54
+ { new : true , upsert : true }
57
55
) ;
58
56
// console.log (user);
59
-
60
-
61
- // Send verification email with OTP
62
- await sendVerificationEmail ( email , otp ) ;
63
57
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 ,
66
62
} ) ;
67
- console . log ( { OTP : otp , Token : tempToken } ) ;
68
63
69
64
res . status ( 201 ) . json ( {
70
65
message : "Verify account using OTP sent to your email" ,
@@ -75,7 +70,66 @@ const register = async (req, res) => {
75
70
res . status ( 500 ) . json ( { message : "Internal server error" } ) ;
76
71
}
77
72
} ;
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
+ } ) ;
78
124
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
+ } ;
79
133
const login = async ( req , res ) => {
80
134
try {
81
135
const { email, password } = req . body ;
@@ -87,13 +141,21 @@ const login = async (req, res) => {
87
141
if ( ! passwordMatch ) {
88
142
return res . status ( 401 ) . json ( { message : "Invalid password" } ) ;
89
143
}
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
+ }
90
152
const token = generateToken ( user ) ;
153
+ console . log ( "Logged In as_________________________" + user . username ) ;
91
154
res . json ( [ { Status : "logged in" } , { user, token } ] ) ;
92
155
} catch ( error ) {
93
156
res . status ( 400 ) . json ( { message : error . message } ) ;
94
157
}
95
158
} ;
96
-
97
159
const showInfo = async ( req , res ) => {
98
160
try {
99
161
// Access userId from req object
@@ -106,11 +168,6 @@ const showInfo = async (req, res) => {
106
168
res . status ( 500 ) . json ( { message : error . message } ) ;
107
169
}
108
170
} ;
109
-
110
- // const showInfo = async (req, res) => {
111
- // res.json("this is showInfo")
112
- // };
113
-
114
171
const changePassword = async ( req , res ) => {
115
172
try {
116
173
const { currentPassword, newPassword } = req . body ;
@@ -140,10 +197,68 @@ const changePassword = async (req, res) => {
140
197
res . status ( 500 ) . json ( { message : "Internal server error" } ) ;
141
198
}
142
199
} ;
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
+ }
143
245
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
+ } ;
144
255
module . exports = {
145
256
register,
257
+ verifyAccount,
258
+ resendOtp,
146
259
login,
147
260
showInfo,
148
261
changePassword,
262
+ forgrtPassword,
263
+ setNewPassword,
149
264
} ;
0 commit comments