summaryrefslogtreecommitdiff
path: root/lib/models
diff options
context:
space:
mode:
authorClaudius2019-05-13 10:55:37 +0200
committerClaudius2019-05-13 14:37:08 +0200
commitdf666dd2140c8955765972230260d6e4bd5de42a (patch)
treef59e1de29e031c5be288e3e0ae099fc86b16c103 /lib/models
parent4b579be93e8ca7f8141ec5d3e8e70a4b9211159c (diff)
getting password hashing into a hook where it could be async
Signed-off-by: Claudius <opensource@amenthes.de>
Diffstat (limited to '')
-rw-r--r--lib/models/user.js20
1 files changed, 14 insertions, 6 deletions
diff --git a/lib/models/user.js b/lib/models/user.js
index 648db73e..bcf3c094 100644
--- a/lib/models/user.js
+++ b/lib/models/user.js
@@ -5,7 +5,7 @@ var scrypt = require('@mlink/scrypt')
// core
var logger = require('../logger')
-var {generateAvatarURL} = require('../letter-avatars')
+var { generateAvatarURL } = require('../letter-avatars')
module.exports = function (sequelize, DataTypes) {
var User = sequelize.define('User', {
@@ -41,11 +41,7 @@ module.exports = function (sequelize, DataTypes) {
}
},
password: {
- type: Sequelize.TEXT,
- set: function (value) {
- var hash = scrypt.kdfSync(value, scrypt.paramsSync(0.1)).toString('hex')
- this.setDataValue('password', hash)
- }
+ type: Sequelize.TEXT
}
}, {
instanceMethods: {
@@ -153,5 +149,17 @@ module.exports = function (sequelize, DataTypes) {
}
})
+ function updatePasswordHashHook (user, options, done) {
+ // suggested way to hash passwords to be able to do this asynchronously:
+ // @see https://github.com/sequelize/sequelize/issues/1821#issuecomment-44265819
+ if (!user.changed('password')) { return done() }
+ const hash = scrypt.kdfSync(user.get('password'), scrypt.paramsSync(0.1)).toString('hex')
+ user.setDataValue('password', hash)
+ done()
+ }
+
+ User.beforeCreate(updatePasswordHashHook)
+ User.beforeUpdate(updatePasswordHashHook)
+
return User
}