Files
PetitTeton/models/user.js

45 lines
1.1 KiB
JavaScript
Raw Normal View History

"use strict";
var bcrypt = require('bcrypt-nodejs');
module.exports = function(sequelize, DataTypes) {
//The id field is auto added and made primary key.
return sequelize.define('User', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false,
autoIncrement: true
},
login: {
type: DataTypes.STRING
},
password: { //Note: The salt should be stored as part of the hash.
type: DataTypes.STRING,
set: function(val) {
this.setDataValue('password', bcrypt.hashSync(val, bcrypt.genSaltSync(8), null));
}
},
admin: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: true
}
}, {
indexes: [
{
unique: true,
fields: ['login']
}
],
freezeTableName: true, // Model tableName will be the same as the model name
//paranoid: true, //Keep deleted data but flag it as deleted
comment: "A system user authorized to access and manipulate the application data.",
instanceMethods: {
isPasswordValid: function(password) {
return bcrypt.compareSync(password, this.password);
}
}
});
};