0
我很难理解这是如何工作的。我了解如何检查和匹配用户纯文本密码。但是,使用bcrypt加密,我需要进行数据库调用来检查密码实际上是否已加密? 这里是我的工作代码:使用Chai来测试POST /用户密码是否被加密
describe('create (POST /users)', function() {
it('succeeds, with encrypted password', function (done) {
chai.request(expressApp)
.post('/users/')
.send({
email: '[email protected]',
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)
})
}
},
测试目前正在通过用户模型的代码,但我知道这是不正确的,可有人帮助请我明白这一点?
谢谢!绝对需要先检查文档。不过,我仍然在努力应对仍需要做的事情。我更新了代码,但我仍然不知道该从哪里下载 –
这就是你需要的全部内容 - 如果bcrypt.compare的结果为true,那么用户中的加密密码与您正在测试的明文密码相匹配。 – dmfay