【发布时间】:2017-03-02 17:24:37
【问题描述】:
我很难理解这是如何工作的。我了解如何检查和匹配用户的纯文本密码。但是,使用 bcrypt 加密,我需要进行数据库调用以检查密码是否实际上已加密? 这是我正在使用的代码:
describe('create (POST /users)', function () {
it('succeeds, with encrypted password', function (done) {
chai.request(expressApp)
.post('/users/')
.send({
email: 'johndoe@gmail.com',
username: 'johndoe',
first_name: 'John',
last_name: 'Doe',
password: '123456789',
phone_number: '+1.888.12456'
})
.end((err, res) => {
res.should.have.status(201)
res.should.be.json
res.body.should.have.property('id', 2)
res.body.should.not.have.property('password')
User.forge({ id: res.body.id })
.fetch()
.then((user) => {
console.log(user.attributes.password) // prints 123456789
return bcrypt.compare(user.attributes.password, res.body.password).then (function (res) {
res.should.equal(true)
})
.fetch()
.catch(err)
done(err)
})
})
这是使用 Bookshelf.js 的用户模型的代码
initialize () {
this.on('saving', this.encryptPassword)
},
encryptPassword (model, attrs, options) {
if (attrs.password) {
return bcrypt.hash(model.attributes.password, 10).then((hash) => {
model.set('password', hash)
})
}
},
测试目前正在通过,但我知道它不正确,有人可以帮助我理解这一点吗?
【问题讨论】:
标签: node.js express mocha.js chai bookshelf.js