add methods for password hashing in User model

Signed-off-by: BinotaLIU <me@binota.org>
This commit is contained in:
BinotaLIU 2020-04-09 02:26:25 +08:00
parent 527c3ae7d9
commit ec206db173
No known key found for this signature in database
GPG Key ID: 87C590ECC1F5314E

View File

@ -1,7 +1,7 @@
'use strict' 'use strict'
// external modules // external modules
var Sequelize = require('sequelize') var Sequelize = require('sequelize')
var scrypt = require('scrypt') var Scrypt = require('scrypt-kdf')
// core // core
var logger = require('../logger') var logger = require('../logger')
@ -41,20 +41,20 @@ module.exports = function (sequelize, DataTypes) {
} }
}, },
password: { password: {
type: Sequelize.TEXT, type: Sequelize.TEXT
set: function (value) {
var hash = scrypt.kdfSync(value, scrypt.paramsSync(0.1)).toString('hex')
this.setDataValue('password', hash)
}
} }
}) })
User.prototype.verifyPassword = function (attempt) { User.prototype.hashPassword = async function (plain) {
if (scrypt.verifyKdfSync(Buffer.from(this.password, 'hex'), attempt)) { return Scrypt.kdf(plain, await Scrypt.pickParams(0.1)).toString('hex')
}
User.prototype.verifyPassword = async function (attempt) {
if (await Scrypt.verifyKdf(Buffer.from(this.password, 'hex'), attempt)) {
return this return this
} else {
return false
} }
return false
} }
User.associate = function (models) { User.associate = function (models) {