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

fix error in user model

parent 656deba9
No related branches found
No related tags found
1 merge request!20Auth
Pipeline #
......@@ -13,7 +13,7 @@ module.exports = () => {
log.debug(`Connecting to MongDB on URI ${mongoUri}`);
const db = mongoose.connect(mongoUri);
mongoose.connection.once('open', () => { log.info("MongoDB connected"); });
mongoose.connection.once('open', () => { log.info('MongoDB connected'); });
return db;
};
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt');
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const Schema = mongoose.Schema;
// set up a mongoose model
var UserSchema = new Schema({
email:{
type: String,
required: true,
unique: true
},
email: {
type: String,
unique: true,
required: true
},
password: {
type: String,
required: true
}
type: String,
required: true
}
});
UserSchema.pre('save', function(next){
var user = this;
if(this.isModified('password') || this.isNew){
bcrypt.genSalt(128, 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.pre('save', function (next) {
var user = this;
if (this.isModified('password') || this.isNew) {
bcrypt.genSalt(128, 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)
});
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