taller6
This commit is contained in:
36
taller6/backend/models/usuario.js
Normal file
36
taller6/backend/models/usuario.js
Normal file
@@ -0,0 +1,36 @@
|
||||
'use strict'
|
||||
const bcrypt = require('bcrypt-nodejs');
|
||||
const mongoose = require('mongoose');
|
||||
const Schema = mongoose.Schema;
|
||||
const UsuarioSchema = Schema({
|
||||
nombre: String,
|
||||
mail: String,
|
||||
pass: String,
|
||||
activo: Boolean
|
||||
});
|
||||
|
||||
UsuarioSchema.pre('save', function(next) {
|
||||
const usuario = this;
|
||||
if (!usuario.isModified('pass')) {
|
||||
return next();
|
||||
}
|
||||
|
||||
if (usuario.activo == null) {
|
||||
usuario.activo = false;
|
||||
}
|
||||
|
||||
bcrypt.genSalt(10, (err, salt) => {
|
||||
if (err) {
|
||||
next(err);
|
||||
}
|
||||
bcrypt.hash(usuario.pass, salt, null, (err, hash) => {
|
||||
if (err) {
|
||||
next(err);
|
||||
}
|
||||
usuario.pass = hash;
|
||||
next();
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
module.exports = mongoose.model('usuario', UsuarioSchema);
|
Reference in New Issue
Block a user