UNPKG

ajinkya-mhetre-mern

Version:

A MERN starter with frontend and backend folders

63 lines (59 loc) 1.44 kB
// ===== src/models/User.js ===== const mongoose = require('mongoose'); const bcrypt = require('bcryptjs'); const userSchema = new mongoose.Schema({ name: { type: String, required: [true, 'Name is required'], trim: true }, email: { type: String, required: [true, 'Email is required'], unique: true, lowercase: true }, password: { type: String, required: [true, 'Password is required'], minlength: 6 }, phone: { type: String, required: [true, 'Phone number is required'] }, role: { type: String, enum: ['farmer', 'customer', 'admin'], required: true }, isVerified: { type: Boolean, default: function() { return this.role === 'customer' || this.role === 'admin'; } }, address: { street: String, city: String, state: String, zipCode: String }, farmDetails: { farmName: String, farmSize: String, farmLocation: String, certifications: [String] } }, { timestamps: true }); userSchema.pre('save', async function(next) { if (!this.isModified('password')) return next(); this.password = await bcrypt.hash(this.password, 12); next(); }); userSchema.methods.comparePassword = async function(candidatePassword) { return await bcrypt.compare(candidatePassword, this.password); }; module.exports = mongoose.model('User', userSchema);