1
1
import bcrypt from "bcrypt" ;
2
2
import User from "../models/User.js" ;
3
3
import jwt from "jsonwebtoken" ;
4
- import dotenv from "dotenv"
4
+ import dotenv from "dotenv" ;
5
5
import nodemailer from "nodemailer" ;
6
- dotenv . config ( )
6
+
7
+ dotenv . config ( ) ;
7
8
/**
8
9
* @route {POST} /api/signup
9
10
* @description Create a new user
@@ -50,7 +51,7 @@ const getAllUserName = async (req, res) => {
50
51
names . forEach ( ( val ) => nameArr . push ( val . username ) ) ;
51
52
res . status ( 200 ) . json ( { usernames : nameArr , success : true } ) ;
52
53
} catch ( error ) {
53
- console . log ( error )
54
+ console . log ( error ) ;
54
55
res . status ( 404 ) . json ( { success : false , message : "Internal server error" } ) ;
55
56
}
56
57
} ;
@@ -84,13 +85,9 @@ const Login = async (req, res) => {
84
85
. json ( { success : false , message : "Incorrect Password" } ) ;
85
86
86
87
// If the password is correct, generate a JWT token
87
- const token = jwt . sign (
88
- { userId : user . _id } ,
89
- process . env . SECRET ,
90
- {
91
- expiresIn : "30d" ,
92
- }
93
- ) ;
88
+ const token = jwt . sign ( { userId : user . _id } , process . env . SECRET , {
89
+ expiresIn : "30d" ,
90
+ } ) ;
94
91
res . status ( 200 ) . json ( { success : true , user : user , token : token } ) ;
95
92
} catch ( error ) {
96
93
console . log ( error ) ;
@@ -107,23 +104,22 @@ const Login = async (req, res) => {
107
104
*/
108
105
const verifyUserByToken = async ( req , res ) => {
109
106
try {
110
- const user = await User . findById ( req . user . userId )
111
- return res . status ( 200 ) . json ( { success : true , user } )
107
+ const user = await User . findById ( req . user . userId ) ;
108
+ return res . status ( 200 ) . json ( { success : true , user } ) ;
112
109
} catch ( error ) {
113
110
console . log ( error ) ;
114
111
return res
115
112
. status ( 404 )
116
113
. json ( { success : false , message : "Internal Server Error" } ) ;
117
114
}
118
-
119
- }
115
+ } ;
120
116
121
117
async function Sendcontactmail ( req , res ) {
122
118
const { name, email, message, rating } = req . body ; // Capture rating from the request
123
119
console . log ( req . body ) ;
124
120
try {
125
121
const transporter = nodemailer . createTransport ( {
126
- service : ' gmail' ,
122
+ service : " gmail" ,
127
123
auth : {
128
124
user : process . env . SMTP_EMAIL ,
129
125
pass : process . env . SMTP_PASSWORD ,
@@ -132,8 +128,8 @@ async function Sendcontactmail(req, res) {
132
128
133
129
// Create a string of stars based on the rating, filled and unfilled stars
134
130
const totalStars = 5 ;
135
- const filledStars = '★' . repeat ( rating ) ; // Filled stars
136
- const emptyStars = '☆' . repeat ( totalStars - rating ) ; // Empty stars
131
+ const filledStars = "★" . repeat ( rating ) ; // Filled stars
132
+ const emptyStars = "☆" . repeat ( totalStars - rating ) ; // Empty stars
137
133
138
134
const mailOptions = {
139
135
from : email ,
@@ -157,20 +153,85 @@ async function Sendcontactmail(req, res) {
157
153
158
154
await transporter . sendMail ( mailOptions ) ;
159
155
160
- return res . status ( 200 ) . json ( { success : true , message : 'Message sent successfully!' } ) ;
156
+ return res
157
+ . status ( 200 )
158
+ . json ( { success : true , message : "Message sent successfully!" } ) ;
161
159
} catch ( error ) {
162
- console . error ( 'Error sending email:' , error ) ;
163
- return res . status ( 500 ) . json ( { success : false , message : 'Error sending email' } ) ;
160
+ console . error ( "Error sending email:" , error ) ;
161
+ return res
162
+ . status ( 500 )
163
+ . json ( { success : false , message : "Error sending email" } ) ;
164
164
}
165
165
}
166
+ const forgotPassword = async function ( req , res ) {
167
+ const { email } = req . body ;
168
+ const user = await User . findOne ( { email } ) ;
169
+ try {
170
+ if ( ! user ) {
171
+ return res
172
+ . status ( 404 )
173
+ . json ( { success : false , message : "Email doesnt exist" } ) ;
174
+ } else {
175
+ const token = jwt . sign ( { userId : user . _id } , process . env . SECRET , {
176
+ expiresIn : "5m" ,
177
+ } ) ;
178
+ var transporter = nodemailer . createTransport ( {
179
+ service : "gmail" ,
180
+ auth : {
181
+ user : process . env . SMTP_EMAIL ,
182
+ pass : process . env . SMTP_PASSWORD ,
183
+ } ,
184
+ } ) ;
185
+ var userFullName = user . firstName + " " + user . lastName ;
186
+ var mailOptions = {
187
+ from : process . env . SMTP_EMAIL ,
188
+ to : email ,
189
+ subject : "Password Reset | TastyTrails" ,
190
+ html : `<p>Hi <b> ${ userFullName } ,</b><br>Use the below link to reset you password. Remember, the link will expire in 10 minutes.<br> ${ process . env . FRONT_END_URL } /reset_password/${ token } ` ,
191
+ } ;
192
+
193
+ transporter . sendMail ( mailOptions , function ( error , info ) {
194
+ if ( error ) {
195
+ return res
196
+ . status ( 404 )
197
+ . json ( { success : false , message : "Email not sent" } ) ;
198
+ } else {
199
+ return res
200
+ . status ( 200 )
201
+ . json ( { success : true , message : "Email sent succesfully" } ) ;
202
+ }
203
+ } ) ;
204
+ }
205
+ } catch ( error ) {
206
+ console . log ( error ) ;
207
+ }
208
+ } ;
166
209
210
+ const resetPassword = async function ( req , res ) {
211
+ const { token } = req . params ;
212
+ const { password } = req . body ;
213
+ try {
214
+ const decoded = jwt . verify ( token , process . env . SECRET ) ;
215
+ console . log ( decoded ) ;
216
+ const id = decoded . userId ;
217
+ const hashPassword = await bcrypt . hash ( password , 10 ) ;
218
+ await User . findByIdAndUpdate ( { _id : id } , { password : hashPassword } ) ;
219
+ return res
220
+ . status ( 200 )
221
+ . json ( { success : true , message : "Password reset succesfully" } ) ;
222
+ } catch ( error ) {
223
+ return res . status ( 400 ) . json ( { success : false , message : "Invalid token" } ) ;
224
+ }
225
+ } ;
167
226
168
227
const UserController = {
169
228
Signup,
170
229
Login,
171
230
getAllUserName,
172
231
verifyUserByToken,
173
- Sendcontactmail
232
+ forgotPassword,
233
+ resetPassword,
234
+ Sendcontactmail,
174
235
} ;
175
236
176
237
export default UserController ;
0 commit comments