@@ -8,34 +8,28 @@ const {
8
8
findUserByEmail,
9
9
findUserById,
10
10
} = require ( "../utils/PasswordTokenAndUser.js" ) ;
11
-
12
11
const sendOTP = async ( req , res ) => {
13
12
try {
14
13
const { email } = req . body ;
15
14
if ( ! email ) {
16
15
return res . status ( 400 ) . json ( {
17
- message : "You Haven 't Entered the Email !" ,
16
+ message : "You haven 't entered the email !" ,
18
17
success : false ,
19
18
} ) ;
20
19
}
21
-
22
20
const studentExists = await User . findOne ( { email } ) ;
23
-
24
21
if ( ! studentExists ) {
25
22
return res . status ( 401 ) . json ( {
26
23
success : false ,
27
24
message : "No user with the given email is registered!" ,
28
25
} ) ;
29
26
}
30
-
31
27
let otp = otpGenerator . generate ( 6 , {
32
28
upperCaseAlphabets : false ,
33
29
lowerCaseAlphabets : false ,
34
30
specialChars : false ,
35
31
} ) ;
36
-
37
32
let result = await OTP . findOne ( { otp } ) ;
38
-
39
33
while ( result ) {
40
34
otp = otpGenerator . generate ( 6 , {
41
35
upperCaseAlphabets : false ,
@@ -44,40 +38,38 @@ const sendOTP = async (req, res) => {
44
38
} ) ;
45
39
result = await OTP . findOne ( { otp } ) ;
46
40
}
47
-
48
41
const otpSent = await OTP . create ( {
49
42
email,
50
43
otp,
51
44
} ) ;
52
-
53
45
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
+ } ) ;
57
50
}
58
-
59
51
const info = await sendMail ( { receiver : email , otp } ) ;
60
52
if ( ! info ) {
61
- console . log ( "Something went wrong while sending email" ) ;
53
+ console . error ( "Something went wrong while sending email" ) ;
62
54
return res . status ( 500 ) . json ( {
63
- message : "Something Went Wrong in mailing the person" ,
55
+ message : "Something went wrong in mailing the person" ,
64
56
success : false ,
65
57
} ) ;
66
58
}
67
-
68
59
return res . status ( 200 ) . json ( {
69
60
success : true ,
70
- message : "OTP Sent Successfully " ,
61
+ message : "OTP sent successfully " ,
71
62
otp,
72
63
} ) ;
73
64
} 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
+ } ) ;
78
71
}
79
72
} ;
80
-
81
73
const verifyOTP = async ( req , res ) => {
82
74
try {
83
75
const { email, otp } = req . body ;
@@ -87,51 +79,37 @@ const verifyOTP = async (req, res) => {
87
79
success : false ,
88
80
} ) ;
89
81
}
90
-
91
82
const otpRecord = await OTP . findOne ( { email, otp } ) ;
92
83
if ( ! otpRecord ) {
93
84
return res . status ( 401 ) . json ( {
94
85
success : false ,
95
- message : "Invalid OTP or Email !" ,
86
+ message : "Invalid OTP or email !" ,
96
87
} ) ;
97
88
}
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
111
89
await OTP . deleteOne ( { email, otp } ) ;
112
90
const existingUser = await findUserByEmail ( email ) ;
113
-
114
91
if ( existingUser ) {
115
92
const tokenReturn = forgotPasswordToken ( existingUser ) ;
116
93
const link = `/api/v1/newPassword/${ existingUser . _id } /${ tokenReturn } ` ;
117
- console . log ( "Link is: " , link ) ;
94
+ console . log ( "Link is:" , link ) ;
118
95
return res . status ( 200 ) . json ( {
119
96
success : true ,
120
- message : "OTP Verified Successfully " ,
97
+ message : "OTP verified successfully " ,
121
98
link : link ,
122
99
} ) ;
123
100
} else {
124
101
return res . status ( 401 ) . json ( {
125
102
success : false ,
126
- message : "The Email cant be found in the database!" ,
103
+ message : "The email can't be found in the database!" ,
127
104
} ) ;
128
105
}
129
106
} 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
+ } ) ;
134
113
}
135
114
} ;
136
-
137
115
module . exports = { sendOTP, verifyOTP } ;
0 commit comments