@@ -14,6 +14,7 @@ const {
1414 sendEmailToAdminVerified,
1515} = require ( "../utils/sendVerificationMail" ) ;
1616const { default : mongoose } = require ( "mongoose" ) ;
17+ const { generateToken } = require ( "../utils/sevices" ) ;
1718
1819async function getChatMaruti ( req , res ) {
1920 try {
@@ -195,42 +196,56 @@ async function userRegistration(req, res) {
195196 const { username, email, password, isVerified } = req . body ;
196197 console . log ( req . body ) ;
197198 try {
199+ // Validate required fields
198200 if ( ! username || ! email || ! password ) {
199201 return res
200202 . status ( 400 )
201203 . json ( { status : "failed" , message : "All fields are required" } ) ;
202204 }
203205
204- const existingUser = await User . findOne ( { email : email } ) ;
206+ // Check if user already exists
207+ const existingUser = await User . findOne ( { email } ) ;
205208 if ( existingUser ) {
206209 return res
207210 . status ( 409 )
208211 . json ( { status : "failed" , message : "Email already exists" } ) ;
209212 }
210213
214+ // Hash the password
211215 const salt = await bcrypt . genSalt ( 10 ) ;
212216 const hashPassword = await bcrypt . hash ( password , salt ) ;
213217
218+ // Create a new user
214219 const newUser = new User ( {
215- username : username ,
216- email : email ,
220+ username,
221+ email,
217222 password : hashPassword ,
218223 isVerified : isVerified ?? false ,
219224 } ) ;
225+
226+ // Send emails for admin and verification
220227 await sendEmailToAdminVerified ( username , email , newUser . _id ) ;
221228 await sendVerificationMail ( username , email , newUser . _id ) ;
222- const userData = await newUser . save ( ) ;
223229
224- // Send verification email
230+ const userData = await newUser . save ( ) ;
225231
226- const token = jwt . sign ( { userId : userData . _id } , process . env . JWT_SECRET , {
227- expiresIn : "10h" ,
228- } ) ;
232+ // Generate tokens
233+ const accessToken = generateToken (
234+ userData . _id ,
235+ process . env . JWT_SECRET ,
236+ "1d"
237+ ) ;
238+ const refreshToken = generateToken (
239+ userData . _id ,
240+ process . env . JWT_REFRESH_SECRET ,
241+ "7d"
242+ ) ;
229243
230244 res . status ( 201 ) . json ( {
231245 status : "success" ,
232- message : "Verification email sent" ,
233- token : token ,
246+ message : "User registered successfully. Verification email sent." ,
247+ accessToken,
248+ refreshToken,
234249 } ) ;
235250 } catch ( error ) {
236251 console . error ( "Error in userRegistration:" , error ) ;
@@ -241,50 +256,68 @@ async function userRegistration(req, res) {
241256async function userLogin ( req , res ) {
242257 try {
243258 const { email, password } = req . body ;
259+
260+ // Validate required fields
244261 if ( ! email || ! password ) {
245262 return res
246263 . status ( 400 )
247264 . json ( { status : "failed" , message : "All fields are required" } ) ;
248265 }
249266
250- const user = await User . findOne ( { email : email } ) ;
267+ // Find the user by email
268+ const user = await User . findOne ( { email } ) ;
251269 if ( ! user ) {
252270 return res
253271 . status ( 404 )
254272 . json ( { status : "failed" , message : "You are not registered" } ) ;
255273 }
274+
275+ // Check if the account is disabled
256276 if ( user . isDisable ) {
257- return res . status ( 403 ) . json ( {
258- status : "failed" ,
259- message : "Access denied. Your account has been disabled. Please contact support for further assistance."
277+ return res . status ( 403 ) . json ( {
278+ status : "failed" ,
279+ message :
280+ "Access denied. Your account has been disabled. Please contact support for further assistance." ,
260281 } ) ;
261282 }
262-
263283
284+ // Verify the password
264285 const isMatch = await bcrypt . compare ( password , user . password ) ;
265286 if ( ! isMatch ) {
266287 return res
267288 . status ( 401 )
268289 . json ( { status : "failed" , message : "Invalid email or password" } ) ;
269290 }
270291
292+ // Check if the email is verified
271293 if ( ! user . isVerified ) {
272294 return res
273295 . status ( 401 )
274- . json ( { status : "failed" , message : "First verify email" } ) ;
296+ . json ( { status : "failed" , message : "Please verify your email first " } ) ;
275297 }
276298
277- // Generate JWT token without expiration time
278- const token = jwt . sign ( { userId : user . _id } , process . env . JWT_SECRET ) ;
299+ // Generate tokens
300+ const accessToken = generateToken ( user . _id , process . env . JWT_SECRET , "1d" ) ;
301+ const refreshToken = generateToken (
302+ user . _id ,
303+ process . env . JWT_REFRESH_SECRET ,
304+ "7d"
305+ ) ;
279306
280307 res . status ( 200 ) . json ( {
281308 status : "success" ,
282- message : "Login Successfully" ,
283- token : token ,
284- user : user ,
309+ message : "Login successful" ,
310+ accessToken,
311+ refreshToken,
312+ user : {
313+ _id : user . _id ,
314+ username : user . username ,
315+ email : user . email ,
316+ isVerified : user . isVerified ,
317+ } ,
285318 } ) ;
286319 } catch ( error ) {
287- console . error ( error ) ;
320+ console . error ( "Error in userLogin:" , error ) ;
288321 res . status ( 500 ) . json ( { status : "failed" , message : "Unable to login" } ) ;
289322 }
290323}
@@ -336,7 +369,34 @@ async function forgotPassword(req, res) {
336369 . json ( { status : "failed" , message : "Unable to process request" } ) ;
337370 }
338371}
372+ async function refreshAccessToken ( req , res ) {
373+ try {
374+ const refreshToken = req . body . refreshToken ; // Assume token in httpOnly cookie
375+ if ( ! refreshToken ) {
376+ return res . status ( 401 ) . json ( { message : "Refresh token missing" } ) ;
377+ }
339378
379+ // Verify refresh token
380+ jwt . verify ( refreshToken , process . env . JWT_REFRESH_SECRET , ( err , payload ) => {
381+ if ( err )
382+ return res . status ( 403 ) . json ( { message : "Invalid refresh token" } ) ;
383+
384+ // Generate a new access token
385+ const accessToken = jwt . sign (
386+ { userId : payload . userId } ,
387+ process . env . JWT_SECRET ,
388+ {
389+ expiresIn : "1d" ,
390+ }
391+ ) ;
392+
393+ res . status ( 200 ) . json ( { accessToken } ) ;
394+ } ) ;
395+ } catch ( error ) {
396+ console . error ( "Error refreshing token:" , error ) ;
397+ res . status ( 500 ) . json ( { message : "Failed to refresh token" } ) ;
398+ }
399+ }
340400async function userPasswordReset ( req , res ) {
341401 const { password, password_confirmation } = req . body ;
342402 const { id, token } = req . params ;
@@ -454,12 +514,12 @@ async function userIsDisable(req, res) {
454514 . status ( 404 )
455515 . json ( { status : "failed" , message : "User not present" } ) ;
456516 }
457- console . log ( req . body . isDisable )
517+ console . log ( req . body . isDisable ) ;
458518 // Check if isDisable is provided in the request body
459519 if ( req . body . hasOwnProperty ( "isDisable" ) ) {
460520 user . isDisable = req . body . isDisable ;
461521 } else {
462- user . isDisable = user . isDisable !== undefined ? user . isDisable : false ;
522+ user . isDisable = user . isDisable !== undefined ? user . isDisable : false ;
463523 }
464524
465525 await user . save ( ) ; // Save the updated user
@@ -644,6 +704,7 @@ async function deleteBook(req, res) {
644704}
645705
646706module . exports = {
707+ refreshAccessToken,
647708 userRegistration,
648709 userLogin,
649710 verifyMail,
0 commit comments