Created
November 16, 2016 10:21
-
-
Save JesusMurF/9d206738aa54131a6e7ac88ab2d9084e to your computer and use it in GitHub Desktop.
How to encrypt password in Sequelize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Sequelize from 'sequelize' | |
import bcrypt from 'bcrypt-nodejs' | |
import connection from '../config/db' | |
require('sequelize-isunique-validator')(Sequelize) | |
let User = connection.define('user', { | |
firstName: { | |
type: Sequelize.STRING(50), | |
allowNull: false, | |
validate: { | |
len: { | |
args: [0, 50], | |
msg: 'El nombre tiene demasiados carácteres' | |
} | |
} | |
}, | |
lastName: { | |
type: Sequelize.STRING(100), | |
allowNull: false, | |
validate: { | |
len: { | |
args: [0, 100], | |
msg: 'Los apellidos tienen demasiados carácteres' | |
} | |
} | |
}, | |
email: { | |
type: Sequelize.STRING(100), | |
allowNull: false, | |
unique: true, | |
validate: { | |
isEmail: { | |
msg: 'No es una dirección de correo electrónico.' | |
}, | |
isUnique: connection.validateIsUnique( | |
'email', | |
'Esta dirección de correo electrónico ya existe.' | |
) | |
} | |
}, | |
password: { | |
type: Sequelize.STRING, | |
allowNull: false | |
} | |
}, { | |
instanceMethods: { | |
generateHash: function (password) { | |
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null) | |
}, | |
validPassword: function (password) { | |
return bcrypt.compareSync(password, this.password) | |
} | |
} | |
}) | |
User.sync() | |
export default User |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@hansaliyad1, because You need to specify the hook correctly and map all the array of the users.
In the
map
function, You will be able to crypt a password.