@@ -17,35 +17,74 @@ const userSchema = new mongoose.Schema(
17
17
type : String ,
18
18
required : true ,
19
19
unique : true ,
20
- match : / ^ [ \w -\. ] + @ ( [ \w - ] + \. ) + [ \w - ] { 2 , 4 } $ / ,
20
+ match : / ^ [ \w -\. ] + @ ( [ \w - ] + \. ) + [ \w - ] { 2 , 4 } $ / , // Regex for email validation
21
21
} ,
22
22
address : { type : String , maxlength : 100 } ,
23
- password : { type : String } ,
23
+ password : { type : String , required : true } , // Password field is required
24
24
role : {
25
25
type : String ,
26
26
enum : [ "admin" , "farmer" , "vendor" , "customer" ] ,
27
- default : "customer" ,
27
+ default : "customer" , // Default role is customer
28
28
} ,
29
29
resetPasswordOTP : { type : String } ,
30
30
resetPasswordExpires : { type : Date } ,
31
31
isVerified : { type : Boolean , default : false } ,
32
32
verificationToken : { type : String } ,
33
33
otp : { type : String } ,
34
+ otpExpires : { type : Date } ,
34
35
phone : { type : String } ,
35
36
profilePicture : { type : String } ,
37
+ googleId : { type : String , sparse : true } , // Sparse index allows for non-unique Google IDs
38
+ rentals : [
39
+ {
40
+ rentalId : { type : String , required : true } ,
41
+ product : { type : mongoose . Schema . Types . ObjectId , ref : "RentProduct" , required : true } ,
42
+ quantity : { type : Number , default : 1 } ,
43
+ rentalDuration : { type : String , required : true } ,
44
+ rentalDate : { type : Date , default : Date . now } ,
45
+ returnDate : { type : Date } ,
46
+ status : {
47
+ type : String ,
48
+ enum : [ "ongoing" , "returned" , "cancelled" , "approved" , "rejected" ] ,
49
+ default : "ongoing" ,
50
+ } ,
51
+ } ,
52
+ ] ,
53
+ wishlist : [
54
+ {
55
+ type : mongoose . Schema . Types . ObjectId ,
56
+ ref : "RentProduct" ,
57
+ } ,
58
+ ] ,
59
+ cart : [
60
+ {
61
+ product : { type : mongoose . Schema . Types . ObjectId , ref : "RentProduct" , required : true } ,
62
+ quantity : { type : Number , default : 1 } ,
63
+ } ,
64
+ ] ,
36
65
} ,
37
- { timestamps : true }
66
+ { timestamps : true } // Timestamps automatically add createdAt and updatedAt
38
67
) ;
39
68
69
+ // Hash the password before saving
70
+ userSchema . pre ( "save" , async function ( next ) {
71
+ if ( ! this . isModified ( "password" ) ) return next ( ) ; // Only hash if the password is modified
72
+ this . password = await bcrypt . hash ( this . password , 10 ) ;
73
+ next ( ) ;
74
+ } ) ;
75
+
76
+ // Method to hash the password
40
77
userSchema . methods . hashPassword = async function ( password ) {
41
- const salt = await bcrypt . genSalt ( 10 ) ;
42
- return bcrypt . hash ( password , salt ) ;
78
+ const salt = await bcrypt . genSalt ( 10 ) ; // Generate salt with 10 rounds
79
+ return bcrypt . hash ( password , salt ) ; // Return the hashed password
43
80
} ;
44
81
82
+ // Method to compare passwords (used for login)
45
83
userSchema . methods . comparePassword = async function ( password ) {
46
- return bcrypt . compare ( password , this . password ) ;
84
+ return bcrypt . compare ( password , this . password ) ; // Compare input password with hashed password
47
85
} ;
48
86
87
+ // Export the User model (will use an existing model if it exists)
49
88
const User = mongoose . models . User || mongoose . model ( "User" , userSchema ) ;
50
89
51
90
module . exports = User ;
0 commit comments