Skip to content

Commit

Permalink
Update user.js
Browse files Browse the repository at this point in the history
  • Loading branch information
haseebzaki-07 authored Nov 10, 2024
1 parent 0a71107 commit 028ceb7
Showing 1 changed file with 46 additions and 7 deletions.
53 changes: 46 additions & 7 deletions backend/model/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,74 @@ const userSchema = new mongoose.Schema(
type: String,
required: true,
unique: true,
match: /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/,
match: /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/, // Regex for email validation
},
address: { type: String, maxlength: 100 },
password: { type: String },
password: { type: String, required: true }, // Password field is required
role: {
type: String,
enum: ["admin", "farmer", "vendor", "customer"],
default: "customer",
default: "customer", // Default role is customer
},
resetPasswordOTP: { type: String },
resetPasswordExpires: { type: Date },
isVerified: { type: Boolean, default: false },
verificationToken: { type: String },
otp: { type: String },
otpExpires: { type: Date },
phone: { type: String },
profilePicture: { type: String },
googleId: { type: String, sparse: true }, // Sparse index allows for non-unique Google IDs
rentals: [
{
rentalId: { type: String, required: true },
product: { type: mongoose.Schema.Types.ObjectId, ref: "RentProduct", required: true },
quantity: { type: Number, default: 1 },
rentalDuration: { type: String, required: true },
rentalDate: { type: Date, default: Date.now },
returnDate: { type: Date },
status: {
type: String,
enum: ["ongoing", "returned", "cancelled", "approved", "rejected"],
default: "ongoing",
},
},
],
wishlist: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "RentProduct",
},
],
cart: [
{
product: { type: mongoose.Schema.Types.ObjectId, ref: "RentProduct", required: true },
quantity: { type: Number, default: 1 },
},
],
},
{ timestamps: true }
{ timestamps: true } // Timestamps automatically add createdAt and updatedAt
);

// Hash the password before saving
userSchema.pre("save", async function (next) {
if (!this.isModified("password")) return next(); // Only hash if the password is modified
this.password = await bcrypt.hash(this.password, 10);
next();
});

// Method to hash the password
userSchema.methods.hashPassword = async function (password) {
const salt = await bcrypt.genSalt(10);
return bcrypt.hash(password, salt);
const salt = await bcrypt.genSalt(10); // Generate salt with 10 rounds
return bcrypt.hash(password, salt); // Return the hashed password
};

// Method to compare passwords (used for login)
userSchema.methods.comparePassword = async function (password) {
return bcrypt.compare(password, this.password);
return bcrypt.compare(password, this.password); // Compare input password with hashed password
};

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

module.exports = User;

0 comments on commit 028ceb7

Please sign in to comment.