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 = () => { ...@@ -13,7 +13,7 @@ module.exports = () => {
log.debug(`Connecting to MongDB on URI ${mongoUri}`); log.debug(`Connecting to MongDB on URI ${mongoUri}`);
const db = mongoose.connect(mongoUri); const db = mongoose.connect(mongoUri);
mongoose.connection.once('open', () => { log.info("MongoDB connected"); }); mongoose.connection.once('open', () => { log.info('MongoDB connected'); });
return db; return db;
}; };
var mongoose = require('mongoose'); const mongoose = require('mongoose');
var Schema = mongoose.Schema; const bcrypt = require('bcrypt');
var bcrypt = require('bcrypt');
const Schema = mongoose.Schema;
// set up a mongoose model
var UserSchema = new Schema({ var UserSchema = new Schema({
email:{ email: {
type: String, type: String,
required: true, unique: true,
unique: true required: true
}, },
password: { password: {
type: String, type: String,
required: true required: true
} }
}); });
UserSchema.pre('save', function(next){ UserSchema.pre('save', function (next) {
var user = this; var user = this;
if(this.isModified('password') || this.isNew){ if (this.isModified('password') || this.isNew) {
bcrypt.genSalt(128, function (err, salt){ bcrypt.genSalt(128, function (err, salt) {
if(err){ if (err) {
return next(err); return next(err);
} }
bcrypt.hash(user.password, salt, function(err, hash){ bcrypt.hash(user.password, salt, function (err, hash) {
if(err){ if (err) {
return next(err); return next(err);
} }
user.password = hash; user.password = hash;
next(); next();
}); });
}); });
} } else {
else{ return next();
return next(); }
}
}); });
UserSchema.methods.comparePassword = function(passw, cb){ UserSchema.methods.comparePassword = function (passw, cb) {
bcrypt.compare(passw, this.password, function(err, isMatch){ bcrypt.compare(passw, this.password, function (err, isMatch) {
if(err){ if (err) {
return cb(err); return cb(err);
} }
cb(null, isMatch) cb(null, isMatch);
}); });
}; };
module.exports = mongoose.model('User', UserSchema); 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