mb672038's picture
می خوام توضیحاتی که دادم واقعی باشه python-learning-platform/
b3c8917 verified
Raw
History Blame Contribute Delete
1.12 kB
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const userSchema = new mongoose.Schema({
username: { type: String, required: true, unique: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
role: { type: String, enum: ['student', 'instructor', 'admin'], default: 'student' },
enrolledCourses: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Course' }],
completedLessons: [{
course: { type: mongoose.Schema.Types.ObjectId, ref: 'Course' },
lesson: { type: mongoose.Schema.Types.ObjectId, ref: 'Lesson' }
}],
profile: {
fullName: String,
bio: String,
avatar: String,
skills: [String]
},
createdAt: { type: Date, default: Date.now }
});
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);