Initial commit
This commit is contained in:
47
server/models/User.js
Normal file
47
server/models/User.js
Normal file
@@ -0,0 +1,47 @@
|
||||
const mongoose = require('mongoose');
|
||||
const bcrypt = require('bcryptjs');
|
||||
|
||||
const addressSchema = new mongoose.Schema({
|
||||
fullName: { type: String, default: '' },
|
||||
mobileNumber: { type: String, default: '' },
|
||||
addressLine1: { type: String, default: '' },
|
||||
addressLine2: { type: String, default: '' },
|
||||
landmark: { type: String, default: '' },
|
||||
street: { type: String, default: '' },
|
||||
city: { type: String, required: true },
|
||||
district: { type: String, default: '' },
|
||||
state: { type: String, required: true },
|
||||
postalCode: { type: String, required: true },
|
||||
country: { type: String, required: true, default: 'India' },
|
||||
isDefault: { type: Boolean, default: false }
|
||||
});
|
||||
|
||||
const userSchema = new mongoose.Schema({
|
||||
name: { type: String, required: [true, 'Please provide your name'], trim: true },
|
||||
email: { type: String, required: [true, 'Please provide your email'], unique: true, lowercase: true, trim: true },
|
||||
password: { type: String, required: [true, 'Please provide a password'], minlength: 6, select: false },
|
||||
role: {
|
||||
type: String,
|
||||
enum: ['Customer', 'Admin', 'Staff', 'Warehouse Manager', 'Order Manager'],
|
||||
default: 'Customer'
|
||||
},
|
||||
phone: { type: String, default: '' },
|
||||
avatar: { type: String, default: 'https://images.unsplash.com/photo-1534528741775-53994a69daeb?w=300' },
|
||||
addresses: [addressSchema],
|
||||
isVerified: { type: Boolean, default: true },
|
||||
resetPasswordToken: String,
|
||||
resetPasswordExpire: Date
|
||||
}, { timestamps: true });
|
||||
|
||||
userSchema.pre('save', async function(next) {
|
||||
if (!this.isModified('password')) return next();
|
||||
const salt = await bcrypt.genSalt(10);
|
||||
this.password = await bcrypt.hash(this.password, salt);
|
||||
next();
|
||||
});
|
||||
|
||||
userSchema.methods.matchPassword = async function(enteredPassword) {
|
||||
return await bcrypt.compare(enteredPassword, this.password);
|
||||
};
|
||||
|
||||
module.exports = mongoose.model('User', userSchema);
|
||||
Reference in New Issue
Block a user