Skip to content
Snippets Groups Projects
Commit 028637d6 authored by Vytor Calixto's avatar Vytor Calixto :space_invader:
Browse files

Revert "Change user model"

This reverts commit 35dcdd88.
parent 5ef2c1dd
No related branches found
No related tags found
1 merge request!116Release v1.0.0
Pipeline #
...@@ -63,13 +63,7 @@ ...@@ -63,13 +63,7 @@
"version" : "v1" "version" : "v1"
} }
}, },
"email": { "email": "simcaq@c3sl.ufpr.br"
"port": 25,
"host": "mx.c3sl.ufpr.br",
"secure": false,
"ignoreTLS": true,
"from": "\"Laboratório de Dados Educacionais\" <lde@c3sl.ufpr.br>"
}
}, },
"production": "production":
{ {
...@@ -99,12 +93,6 @@ ...@@ -99,12 +93,6 @@
"version" : "v1" "version" : "v1"
} }
}, },
"email": { "email": "simcaq@c3sl.ufpr.br"
"port": 25,
"host": "mx.c3sl.ufpr.br",
"secure": false,
"ignoreTLS": true,
"from": "\"Laboratório de Dados Educacionais\" <lde@c3sl.ufpr.br>"
}
} }
} }
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
"dependencies": { "dependencies": {
"agenda": "^0.9.1", "agenda": "^0.9.1",
"apicache": "0.7.0", "apicache": "0.7.0",
"bcrypt-nodejs": "0.0.3",
"body-parser": "^1.13.1", "body-parser": "^1.13.1",
"chai": "^3.5.0", "chai": "^3.5.0",
"chai-http": "^3.0.0", "chai-http": "^3.0.0",
......
const libs = `${process.cwd()}/libs`;
const log = require(`${libs}/log`)(module);
const config = require(`${libs}/config`);
const nodemailer = require('nodemailer');
const htmlToText = require('nodemailer-html-to-text').htmlToText;
let transporter = nodemailer.createTransport({
host: config.email.host,
port: config.email.port,
secure: config.email.secure,
ignoreTLS: config.email.ignoreTLS
});
transporter.use('compile', htmlToText());
// verify connection configuration
transporter.verify(function(error, success) {
if (error) {
log.error(error);
} else {
log.info('Email server is ready to take our messages');
}
});
let mailOptions = {
from: config.email.from
};
module.exports = function send(options, cb) {
Object.assign(options, mailOptions);
transporter.sendMail(options, (err, info) => {
if(err) {
return cb(err);
}
cb(null, info);
});
};
const mongoose = require('mongoose'); const mongoose = require('mongoose');
var crypto = require('crypto') const bcrypt = require('bcrypt-nodejs');
const libs = `${process.cwd()}/libs`; const libs = `${process.cwd()}/libs`;
const log = require(`${libs}/log`)(module); const log = require(`${libs}/log`)(module);
const Schema = mongoose.Schema; const Schema = mongoose.Schema;
...@@ -11,14 +11,10 @@ var UserSchema = new Schema({ ...@@ -11,14 +11,10 @@ var UserSchema = new Schema({
unique: true, unique: true,
required: [true, 'O campo Email é obrigatório.'] required: [true, 'O campo Email é obrigatório.']
}, },
hashedPassword: { password: {
type: String, type: String,
required: [true, 'O campo Senha é obrigatório.'] required: [true, 'O campo Senha é obrigatório.']
}, },
salt: {
type: String,
required: true
},
name: { name: {
type: String, type: String,
required: [true, 'O campo Nome é obrigatório.'] required: [true, 'O campo Nome é obrigatório.']
...@@ -57,23 +53,37 @@ var UserSchema = new Schema({ ...@@ -57,23 +53,37 @@ var UserSchema = new Schema({
}, },
receive_emails: { receive_emails: {
type: Boolean type: Boolean
},
createdAt: {
type: Date,
default: Date.now
} }
});
UserSchema.methods.encryptPassword = (password) => { });
return crypto.pbkdf2Sync(password, this.salt, 10000, 512);
};
UserSchema.virtual('password').set((password) => { UserSchema.pre('save', function (next) {
this._plainPassword = password; var user = this;
this.salt = crypto.randomBytes(128).toString('hex'); if (this.isModified('password') || this.isNew) {
this.hashedPassword = this.encryptPassword(password); bcrypt.genSalt(10, function (err, salt) {
}).get(() => { if (err) {
return this._plainPassword; return next(err);
}
bcrypt.hash(user.password, salt, null, 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); 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