mc-server / models /User.js
arifian853's picture
Initial commit
5df662e
Raw History Blame Contribute Delete
804 Bytes
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const userSchema = new mongoose.Schema({
username: {
type: String,
required: true,
unique: true,
trim: true
},
password: {
type: String,
required: true
},
role: {
type: String,
enum: ['admin'],
default: 'admin'
}
}, {
timestamps: true
});
// Hash password before saving
userSchema.pre('save', async function() {
if (!this.isModified('password')) return;
const salt = await bcrypt.genSalt(10);
this.password = await bcrypt.hash(this.password, salt);
});
// Compare password method
userSchema.methods.comparePassword = async function(candidatePassword) {
return bcrypt.compare(candidatePassword, this.password);
};
module.exports = mongoose.model('User', userSchema);