new procurement
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
const mongoose = require('mongoose')
|
||||
const bcrypt = require('bcryptjs')
|
||||
const mongoose = require('mongoose');
|
||||
const bcrypt = require('bcryptjs');
|
||||
|
||||
const userSchema = new mongoose.Schema({
|
||||
email: {
|
||||
@@ -37,31 +37,37 @@ const userSchema = new mongoose.Schema({
|
||||
type: Date,
|
||||
default: Date.now
|
||||
}
|
||||
})
|
||||
}, {
|
||||
collection: 'users',
|
||||
minimize: false,
|
||||
toObject: { versionKey: false }
|
||||
});
|
||||
|
||||
userSchema.set('toObject', { virtuals: false, versionKey: false });
|
||||
|
||||
// Хешировать пароль перед сохранением
|
||||
userSchema.pre('save', async function(next) {
|
||||
if (!this.isModified('password')) return next()
|
||||
if (!this.isModified('password')) return next();
|
||||
|
||||
try {
|
||||
const salt = await bcrypt.genSalt(10)
|
||||
this.password = await bcrypt.hash(this.password, salt)
|
||||
next()
|
||||
const salt = await bcrypt.genSalt(10);
|
||||
this.password = await bcrypt.hash(this.password, salt);
|
||||
next();
|
||||
} catch (error) {
|
||||
next(error)
|
||||
next(error);
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
// Метод для сравнения паролей
|
||||
userSchema.methods.comparePassword = async function(candidatePassword) {
|
||||
return await bcrypt.compare(candidatePassword, this.password)
|
||||
}
|
||||
return await bcrypt.compare(candidatePassword, this.password);
|
||||
};
|
||||
|
||||
// Скрыть пароль при преобразовании в JSON
|
||||
userSchema.methods.toJSON = function() {
|
||||
const obj = this.toObject()
|
||||
delete obj.password
|
||||
return obj
|
||||
}
|
||||
const obj = this.toObject();
|
||||
delete obj.password;
|
||||
return obj;
|
||||
};
|
||||
|
||||
module.exports = mongoose.model('User', userSchema)
|
||||
module.exports = mongoose.model('User', userSchema);
|
||||
|
||||
Reference in New Issue
Block a user