1
1
// Imports from node modules
2
2
const { v4 : UUIDV4 } = require ( "uuid" ) ;
3
+ const bcrypt = require ( "bcryptjs" ) ;
4
+ const moment = require ( "moment" ) ;
5
+ const { v4 : uuidv4 } = require ( "uuid" ) ;
6
+ const sequelize = require ( "../models" ) . sequelize ;
3
7
4
8
// Local imports
5
9
const logger = require ( "../config/logger" ) ;
@@ -18,7 +22,14 @@ const UserRoles = models.UserRoles;
18
22
const user_application = models . user_application ;
19
23
const NotificationQueue = models . notification_queue ;
20
24
const AccountVerificationCodes = models . AccountVerificationCodes ;
25
+ const PasswordResetLinks = models . PasswordResetLinks ;
21
26
27
+ const {
28
+ checkPasswordSecurityViolations,
29
+ setPasswordExpiry,
30
+ setPreviousPasswords,
31
+ generatePassword,
32
+ } = require ( "../utils/authUtil" ) ;
22
33
23
34
// Delete user with ID
24
35
const deleteUser = async ( req , res ) => {
@@ -407,12 +418,7 @@ const createUser = async (req, res) => {
407
418
}
408
419
409
420
// Generate random password - 12 characters - alpha numeric
410
- const charset =
411
- "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ;
412
- let password = "" ;
413
- for ( let i = 0 ; i < 12 ; i ++ ) {
414
- password += charset . charAt ( Math . floor ( Math . random ( ) * charset . length ) ) ;
415
- }
421
+ const password = generatePassword ( ) ;
416
422
417
423
// Hash password
418
424
const salt = bcrypt . genSaltSync ( 10 ) ;
@@ -467,7 +473,7 @@ const createUser = async (req, res) => {
467
473
} ) ;
468
474
469
475
// Searchable notification ID
470
- const searchableNotificationId = UUIDV4 ( ) ;
476
+ const searchableNotificationId = `USR_REG__ ${ moment ( ) . format ( 'YYYYMMDD_HHmmss_SSS' ) } ` ;
471
477
const verificationCode = UUIDV4 ( ) ;
472
478
473
479
// Create account verification code
@@ -515,6 +521,90 @@ const createUser = async (req, res) => {
515
521
}
516
522
} ;
517
523
524
+ // Reset password for user
525
+ const resetPasswordForUser = async ( req , res ) => {
526
+ const transaction = await sequelize . transaction ( ) ; // Start a transaction
527
+
528
+ try {
529
+ const { id } = req . body ;
530
+
531
+ // Get user by ID
532
+ const user = await User . findOne ( { where : { id } , transaction } ) ;
533
+
534
+ // If user not found
535
+ if ( ! user ) {
536
+ await transaction . rollback ( ) ; // Rollback if user is not found
537
+ return res
538
+ . status ( 404 )
539
+ . json ( { success : false , message : "User not found" } ) ;
540
+ }
541
+
542
+ // Generate a password reset token
543
+ const randomId = uuidv4 ( ) ;
544
+ const passwordRestLink = `${ trimURL ( process . env . WEB_URL ) } /reset-password/${ randomId } ` ;
545
+
546
+ // Searchable notification ID
547
+ const searchableNotificationId = `USR_PWD_RST__${ moment ( ) . format ( "YYYYMMDD_HHmmss_SSS" ) } ` ;
548
+
549
+ // Queue notification
550
+ await NotificationQueue . create (
551
+ {
552
+ type : "email" ,
553
+ templateName : "resetPasswordLink" ,
554
+ notificationOrigin : "Reset Password" ,
555
+ deliveryType : "immediate" ,
556
+ createdBy : "System" ,
557
+ updatedBy : "System" ,
558
+ metaData : {
559
+ notificationId : searchableNotificationId ,
560
+ recipientName : `${ user . firstName } ` ,
561
+ notificationOrigin : "Reset Password" ,
562
+ subject : "Password Reset Link" ,
563
+ mainRecipients : [ user . email ] ,
564
+ notificationDescription : "Password Reset Link" ,
565
+ validForHours : 24 ,
566
+ passwordRestLink,
567
+ } ,
568
+ } ,
569
+ { transaction }
570
+ ) ;
571
+
572
+ // Save the password reset token to the user object in the database
573
+ await PasswordResetLinks . create (
574
+ {
575
+ id : randomId ,
576
+ userId : user . id ,
577
+ resetLink : passwordRestLink ,
578
+ issuedAt : new Date ( ) ,
579
+ expiresAt : new Date ( new Date ( ) . getTime ( ) + 24 * 60 * 60 * 1000 ) ,
580
+ } ,
581
+ { transaction }
582
+ ) ;
583
+
584
+ // Create account verification code
585
+ await AccountVerificationCodes . create (
586
+ {
587
+ code : randomId ,
588
+ userId : user . id ,
589
+ expiresAt : new Date ( new Date ( ) . getTime ( ) + 24 * 60 * 60 * 1000 ) ,
590
+ } ,
591
+ { transaction }
592
+ ) ;
593
+
594
+ // Commit transaction if everything is successful
595
+ await transaction . commit ( ) ;
596
+
597
+ // Response
598
+ res . status ( 200 ) . json ( { success : true , message : "Password reset successfully" } ) ;
599
+ } catch ( err ) {
600
+ await transaction . rollback ( ) ; // Rollback transaction in case of error
601
+ logger . error ( `Reset password for user: ${ err . message } ` ) ;
602
+ res
603
+ . status ( err . status || 500 )
604
+ . json ( { success : false , message : err . message } ) ;
605
+ }
606
+ } ;
607
+
518
608
//Exports
519
609
module . exports = {
520
610
createUser,
@@ -527,4 +617,5 @@ module.exports = {
527
617
bulkUpdateUsers,
528
618
updateUserRoles,
529
619
updateUserApplications,
620
+ resetPasswordForUser,
530
621
} ;
0 commit comments