Files
gancio/server/api/models/user.js

39 lines
978 B
JavaScript
Raw Normal View History

2019-06-06 23:54:32 +02:00
'use strict';
2019-04-03 00:25:12 +02:00
const bcrypt = require('bcrypt')
2019-06-06 23:54:32 +02:00
module.exports = (sequelize, DataTypes) => {
const user = sequelize.define('user', {
email: {
type: DataTypes.STRING,
unique: { msg: 'err.register_error' },
index: true,
allowNull: false
},
description: DataTypes.TEXT,
password: DataTypes.STRING,
recover_code: DataTypes.STRING,
is_admin: DataTypes.BOOLEAN,
is_active: DataTypes.BOOLEAN
}, {});
2019-04-03 00:25:12 +02:00
2019-06-06 23:54:32 +02:00
user.associate = function(models) {
// associations can be defined here
user.hasMany(models.event)
};
2019-04-03 00:25:12 +02:00
2019-06-06 23:54:32 +02:00
user.prototype.comparePassword = async function (pwd) {
if (!this.password) return false
const ret = await bcrypt.compare(pwd, this.password)
return ret
2019-04-03 00:25:12 +02:00
}
2019-06-06 23:54:32 +02:00
user.beforeSave(async (user, options) => {
if (user.changed('password')) {
const salt = await bcrypt.genSalt(10)
const hash = await bcrypt.hash(user.password, salt)
user.password = hash
}
})
2019-04-03 00:25:12 +02:00
2019-06-06 23:54:32 +02:00
return user;
};