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'
// external modules
var Sequelize = require('sequelize')
var scrypt = require('scrypt')
var Scrypt = require('scrypt-kdf')
// core
var logger = require('../logger')
@ -41,20 +41,20 @@ 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
}
})
User.prototype.verifyPassword = function (attempt) {
if (scrypt.verifyKdfSync(Buffer.from(this.password, 'hex'), attempt)) {
User.prototype.hashPassword = async function (plain) {
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
} else {
return false
}
return false
}
User.associate = function (models) {