2017-10-30 01:10:37 +01:00
|
|
|
'use strict';
|
|
|
|
const bcrypt = require('bcrypt');
|
|
|
|
const logger = require('winston');
|
|
|
|
|
2017-09-17 02:50:22 +02:00
|
|
|
module.exports = (sequelize, { STRING }) => {
|
2017-09-15 23:41:47 +02:00
|
|
|
const User = sequelize.define(
|
2017-09-17 02:50:22 +02:00
|
|
|
'User',
|
2017-09-15 23:41:47 +02:00
|
|
|
{
|
2017-09-26 05:30:45 +02:00
|
|
|
userName: {
|
2017-09-15 23:41:47 +02:00
|
|
|
type : STRING,
|
|
|
|
allowNull: false,
|
|
|
|
},
|
|
|
|
password: {
|
|
|
|
type : STRING,
|
|
|
|
allowNull: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
freezeTableName: true,
|
|
|
|
}
|
2017-09-18 19:14:06 +02:00
|
|
|
);
|
2017-09-15 23:41:47 +02:00
|
|
|
|
|
|
|
User.associate = db => {
|
2017-09-26 07:49:27 +02:00
|
|
|
User.hasOne(db.Channel);
|
2017-09-15 23:41:47 +02:00
|
|
|
};
|
|
|
|
|
2018-03-06 01:37:22 +01:00
|
|
|
User.prototype.comparePassword = function (password) {
|
|
|
|
return bcrypt.compare(password, this.password);
|
2017-09-18 19:14:06 +02:00
|
|
|
};
|
|
|
|
|
2017-11-30 23:46:32 +01:00
|
|
|
User.prototype.changePassword = function (newPassword) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// generate a salt string to use for hashing
|
|
|
|
bcrypt.genSalt((saltError, salt) => {
|
|
|
|
if (saltError) {
|
|
|
|
logger.error('salt error', saltError);
|
|
|
|
reject(saltError);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// generate a hashed version of the user's password
|
|
|
|
bcrypt.hash(newPassword, salt, (hashError, hash) => {
|
|
|
|
// if there is an error with the hash generation return the error
|
|
|
|
if (hashError) {
|
|
|
|
logger.error('hash error', hashError);
|
|
|
|
reject(hashError);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// replace the current password with the new hash
|
|
|
|
this
|
|
|
|
.update({password: hash})
|
|
|
|
.then(() => {
|
|
|
|
resolve();
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-30 01:10:37 +01:00
|
|
|
// pre-save hook method to hash the user's password before the user's info is saved to the db.
|
|
|
|
User.hook('beforeCreate', (user, options) => {
|
2017-11-30 23:46:32 +01:00
|
|
|
logger.debug('User.beforeCreate hook...');
|
2017-10-30 01:10:37 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// generate a salt string to use for hashing
|
|
|
|
bcrypt.genSalt((saltError, salt) => {
|
|
|
|
if (saltError) {
|
|
|
|
logger.error('salt error', saltError);
|
|
|
|
reject(saltError);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// generate a hashed version of the user's password
|
|
|
|
bcrypt.hash(user.password, salt, (hashError, hash) => {
|
|
|
|
// if there is an error with the hash generation return the error
|
|
|
|
if (hashError) {
|
|
|
|
logger.error('hash error', hashError);
|
|
|
|
reject(hashError);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// replace the password string with the hash password value
|
|
|
|
user.password = hash;
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-09-15 23:41:47 +02:00
|
|
|
return User;
|
|
|
|
};
|