Download src/models/User.js from eznd/woods: direct link, hf CLI and curl.
- Browser
- Download file 1.28 kB
-
https://huggingface.co/spaces/eznd/woods/resolve/main/src/models/User.js
- Command line
-
hf download hf://spaces/eznd/woods/src/models/User.js
-
curl -L -o User.js https://huggingface.co/spaces/eznd/woods/resolve/main/src/models/User.js
1.28 kB
| import mongoose from 'mongoose'; | |
| import bcrypt from 'bcryptjs'; | |
| const userSchema = new mongoose.Schema({ | |
| name: { type: String, required: true, trim: true }, | |
| email: { type: String, required: true, unique: true, lowercase: true, trim: true }, | |
| password: { type: String, required: true, minlength: 6 }, | |
| role: { type: String, enum: ['admin', 'staff', 'customer'], default: 'customer' }, | |
| phone: { type: String, trim: true }, | |
| isActive: { type: Boolean, default: true }, | |
| lastLogin: { type: Date }, | |
| // Customer specific | |
| loyaltyPoints: { type: Number, default: 0 }, | |
| orderHistory: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Order' }], | |
| // Notification prefs | |
| notifyRestock: { type: Boolean, default: true }, | |
| notifyOrderReady: { type: Boolean, default: true }, | |
| }, { 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 bcrypt.compare(candidatePassword, this.password); | |
| }; | |
| userSchema.methods.toSafeObject = function () { | |
| const obj = this.toObject(); | |
| delete obj.password; | |
| return obj; | |
| }; | |
| export default mongoose.model('User', userSchema); | |