Skip to content
Snippets Groups Projects
Commit 08ee1c37 authored by Lucas Gabriel Lima's avatar Lucas Gabriel Lima
Browse files

add user model and password encryptation

parent 1062775c
No related branches found
No related tags found
1 merge request!20Auth
Pipeline #
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt');
var UserSchema = new Schema({
email:{
type: String,
required: true
},
password: {
type: String,
required: true
}
});
UserSchema.pre('save', function(next){
var user = this;
if(this.isModified('password') || this.isNew){
bcrypt.genSalt(10, function (err, salt){
if(err){
return next(err);
}
bcrypt.hash(user.password, salt, function(err, hash){
if(err){
return next(err);
}
user.password = hash;
next();
});
});
}
else{
return next();
}
});
UserSchema.methods.comparePassword = function(passw, cb){
bcrypt.compare(passw, this.password, function(err, isMatch){
if(err){
return cb(err);
}
cb(null, isMatch)
});
};
module.exports = mongoose.model('User', UserSchema);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment