-
Notifications
You must be signed in to change notification settings - Fork 6
/
ModelUsuario.js
35 lines (35 loc) · 855 Bytes
/
ModelUsuario.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');
//1
var UsuarioSchema = new mongoose.Schema({
username: {
type: String,
unique: true,
required: true
},
password: {
type: String,
required: true
}
});
//2
UsuarioSchema.pre('save', function(next) {
var user = this;
if (!user.isModified('password')) return next();
bcrypt.genSalt(5, function(err, salt) {
if (err) return next(err);
bcrypt.hash(user.password, salt, null, function(err, hash) {
if (err) return next(err);
user.password = hash;
next();
});
});
});
//3
UsuarioSchema.methods.verificaSenha = function(password, next) {
bcrypt.compare(password, this.password, function(err, isMatch) {
if (err) return next(err);
next(isMatch);
});
};
module.exports = mongoose.model('Usuario', UsuarioSchema);