@@ -97,10 +97,22 @@ const verifyToken = async (req, res) => {
97
97
}
98
98
} ;
99
99
100
- async function ResetPasswordByEmail ( req , resp ) {
101
- const VITE_CLIENT_PORT = process . env . VITE_CLIENT_PORT || "https://bitbox-in.netlify.app" ;
100
+ async function ResetPasswordByEmail ( req , res ) {
102
101
102
+ const VITE_CLIENT_PORT = process . env . VITE_CLIENT_PORT || "https://bitbox-in.netlify.app" ;
103
103
const { email } = req . body ;
104
+
105
+ // Check if email is provided
106
+ if ( ! email ) {
107
+ return res . status ( 400 ) . json ( { error : 'Email is required' } ) ;
108
+ }
109
+
110
+ const user = await User . findOne ( { email } ) ;
111
+
112
+ if ( ! user ) {
113
+ return res . status ( 404 ) . json ( { error : 'User does not exist' } ) ;
114
+ }
115
+
104
116
const transporter = nodemailer . createTransport ( {
105
117
service : "gmail" ,
106
118
auth : {
@@ -112,24 +124,23 @@ async function ResetPasswordByEmail(req, resp) {
112
124
const mailOptions = {
113
125
from : process . env . EMAIL_USER ,
114
126
to : email ,
115
- subject : "Reset Your password on BitBox" ,
127
+ subject : "Reset Your Password on BitBox" ,
116
128
html : `
117
- <p>Reset your password using the link below:</p>
118
- <a href="${ VITE_CLIENT_PORT } /reset-password"><button>Click here</button></a> to reset your password
119
- ` ,
129
+ <p>Reset your password using the link below:</p>
130
+ <a href="${ VITE_CLIENT_PORT } /reset-password"><button>Click here</button></a> to reset your password
131
+ ` ,
120
132
} ;
121
133
122
134
transporter . sendMail ( mailOptions , ( error , info ) => {
123
135
if ( error ) {
124
- console . log ( "Error sending email: " + error ) ;
125
- resp . status ( 500 ) . send ( "Error sending email" ) ;
136
+ return res . status ( 500 ) . json ( { error : "Error sending email" } ) ;
126
137
} else {
127
- console . log ( "Email sent: " + info . response ) ;
128
- resp . status ( 200 ) . send ( { message : "email sent successfully" } ) ;
138
+ return res . status ( 200 ) . json ( { message : "Email sent successfully" } ) ;
129
139
}
130
140
} ) ;
131
141
}
132
142
143
+
133
144
const forgetpassword = async ( req , res ) => {
134
145
try {
135
146
const { email, password } = req . body ;
0 commit comments