Skip to content

Commit ec206db

Browse files
committed
add methods for password hashing in User model
Signed-off-by: BinotaLIU <me@binota.org>
1 parent 527c3ae commit ec206db

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

lib/models/user.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22
// external modules
33
var Sequelize = require('sequelize')
4-
var scrypt = require('scrypt')
4+
var Scrypt = require('scrypt-kdf')
55

66
// core
77
var logger = require('../logger')
@@ -41,20 +41,20 @@ module.exports = function (sequelize, DataTypes) {
4141
}
4242
},
4343
password: {
44-
type: Sequelize.TEXT,
45-
set: function (value) {
46-
var hash = scrypt.kdfSync(value, scrypt.paramsSync(0.1)).toString('hex')
47-
this.setDataValue('password', hash)
48-
}
44+
type: Sequelize.TEXT
4945
}
5046
})
5147

52-
User.prototype.verifyPassword = function (attempt) {
53-
if (scrypt.verifyKdfSync(Buffer.from(this.password, 'hex'), attempt)) {
48+
User.prototype.hashPassword = async function (plain) {
49+
return Scrypt.kdf(plain, await Scrypt.pickParams(0.1)).toString('hex')
50+
}
51+
52+
User.prototype.verifyPassword = async function (attempt) {
53+
if (await Scrypt.verifyKdf(Buffer.from(this.password, 'hex'), attempt)) {
5454
return this
55-
} else {
56-
return false
5755
}
56+
57+
return false
5858
}
5959

6060
User.associate = function (models) {

0 commit comments

Comments
 (0)