You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
636 B
34 lines
636 B
6 months ago
|
import mongoose from 'mongoose';
|
||
|
import bcrypt from 'bcryptjs';
|
||
|
|
||
|
const userSchema = new mongoose.Schema({
|
||
|
email: {
|
||
|
type: String,
|
||
|
required: true,
|
||
|
unique: true,
|
||
|
},
|
||
|
password: {
|
||
|
type: String,
|
||
|
required: true
|
||
|
},
|
||
|
name: String,
|
||
|
role: {
|
||
|
type: String,
|
||
|
enum: ['user', 'admin'],
|
||
|
default: 'user'
|
||
|
},
|
||
|
createdAt: {
|
||
|
type: Date,
|
||
|
default: Date.now,
|
||
|
}
|
||
|
});
|
||
|
|
||
|
// Hash password before saving
|
||
|
userSchema.pre('save', async function(next) {
|
||
|
if (this.isModified('password')) {
|
||
|
this.password = await bcrypt.hash(this.password, 12);
|
||
|
}
|
||
|
next();
|
||
|
});
|
||
|
|
||
|
export default mongoose.model('User', userSchema);
|