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 @@
"version" : "v1"
}
},
"email": {
"port": 25,
"host": "mx.c3sl.ufpr.br",
"secure": false,
"ignoreTLS": true,
"from": "\"Laboratório de Dados Educacionais\" <lde@c3sl.ufpr.br>"
}
"email": "simcaq@c3sl.ufpr.br"
},
"production":
{
......@@ -99,12 +93,6 @@
"version" : "v1"
}
},
"email": {
"port": 25,
"host": "mx.c3sl.ufpr.br",
"secure": false,
"ignoreTLS": true,
"from": "\"Laboratório de Dados Educacionais\" <lde@c3sl.ufpr.br>"
}
"email": "simcaq@c3sl.ufpr.br"
}
}
......@@ -15,6 +15,7 @@
"dependencies": {
"agenda": "^0.9.1",
"apicache": "0.7.0",
"bcrypt-nodejs": "0.0.3",
"body-parser": "^1.13.1",
"chai": "^3.5.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');
var crypto = require('crypto')
const bcrypt = require('bcrypt-nodejs');
const libs = `${process.cwd()}/libs`;
const log = require(`${libs}/log`)(module);
const Schema = mongoose.Schema;
......@@ -11,14 +11,10 @@ var UserSchema = new Schema({
unique: true,
required: [true, 'O campo Email é obrigatório.']
},
hashedPassword: {
password: {
type: String,
required: [true, 'O campo Senha é obrigatório.']
},
salt: {
type: String,
required: true
},
name: {
type: String,
required: [true, 'O campo Nome é obrigatório.']
......@@ -57,23 +53,37 @@ var UserSchema = new Schema({
},
receive_emails: {
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) => {
this._plainPassword = password;
this.salt = crypto.randomBytes(128).toString('hex');
this.hashedPassword = this.encryptPassword(password);
}).get(() => {
return this._plainPassword;
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, 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);
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