Skip to content

Commit 028ceb7

Browse files
Update user.js
1 parent 0a71107 commit 028ceb7

File tree

1 file changed

+46
-7
lines changed

1 file changed

+46
-7
lines changed

backend/model/user.js

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,74 @@ const userSchema = new mongoose.Schema(
1717
type: String,
1818
required: true,
1919
unique: true,
20-
match: /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/,
20+
match: /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/, // Regex for email validation
2121
},
2222
address: { type: String, maxlength: 100 },
23-
password: { type: String },
23+
password: { type: String, required: true }, // Password field is required
2424
role: {
2525
type: String,
2626
enum: ["admin", "farmer", "vendor", "customer"],
27-
default: "customer",
27+
default: "customer", // Default role is customer
2828
},
2929
resetPasswordOTP: { type: String },
3030
resetPasswordExpires: { type: Date },
3131
isVerified: { type: Boolean, default: false },
3232
verificationToken: { type: String },
3333
otp: { type: String },
34+
otpExpires: { type: Date },
3435
phone: { type: String },
3536
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+
],
3665
},
37-
{ timestamps: true }
66+
{ timestamps: true } // Timestamps automatically add createdAt and updatedAt
3867
);
3968

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
4077
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
4380
};
4481

82+
// Method to compare passwords (used for login)
4583
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
4785
};
4886

87+
// Export the User model (will use an existing model if it exists)
4988
const User = mongoose.models.User || mongoose.model("User", userSchema);
5089

5190
module.exports = User;

0 commit comments

Comments
 (0)