Skip to content
Snippets Groups Projects
Commit 2f047a37 authored by V S Tharunika's avatar V S Tharunika
Browse files

adding encryption/decryption to password

parent 41839b34
Branches
No related tags found
No related merge requests found
......@@ -12,7 +12,7 @@ app.use(cors())
require('./routes')(app)
sequelize.sync({force: true})
sequelize.sync()
.then (() =>{
app.listen(config.port)
console.log('server started on port ${config.port}')
......
......@@ -35,7 +35,7 @@ module.exports={
error: 'The login information was incorrect'
})
}
const isPasswordValid = password === user.password
const isPasswordValid = await user.comparePassword(password)
if(!isPasswordValid) {
return res.status(403).send({
error: 'The login information was incorrect'
......
const Promise = require('bluebird')
const bcrypt = Promise.promisifiyAll(require('bcrypt-nodejs'))
module.exports =(sequelize, DataTypes) =>
sequelize.define('user',{
function hashPassword (user, options) {
const SALT_FACTOR = 8
if(!user.changed('password')){
return;
}
return bcrypt
.genSaltAsync(SALT_FACTOR)
.then(salt => bcrypt.hashAsync(user.password, salt, null))
.then(hash => {
user.setDataValue('password', hash)
})
}
module.exports =(sequelize, DataTypes) =>{
const User = sequelize.define('user',{
email:{
type:DataTypes.STRING,
unique: true
},
password: DataTypes.STRING
}, {
hooks: {
beforeCreate: hashPassword,
beforeUpdate: hashPassword,
beforeSave: hashPassword
}
})
User.prototype.comparePassword = function (password) {
return bcrypt.compareAsync(password, this.password)
}
return User
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment