如何解码和验证羽毛js
问题描述:
我是feathersJs的新手,并试图学习如何使用钩子和服务来执行验证。我正在使用Couchdb数据库和摇篮。 这是使用“用户”挂钩服务在hashPassword中加密密码的post方法。 POST方法如下:如何解码和验证羽毛js
app.post('/dev',function(req,res,next){
var username = req.body.username;
var password = req.body.password;
app.service('database').create({username,password}).then(user => {
db.save(user, function (err, docs) {
// Handle response
res.json(docs);
});
console.log('User Created Successfully.', user);
}).catch(console.error);
})
和服务是:
app.service('authentication').hooks({
before: {
create: [
// You can chain multiple strategies
auth.hooks.authenticate(['jwt', 'local'])
],
remove: [
auth.hooks.authenticate('jwt')
]
}
});
app.service('database').hooks({
before: {
find: [
auth.hooks.authenticate('jwt')
],
create: [
local.hooks.hashPassword({ passwordField: 'password' })
]
}
});
我现在用的这个以检索数据:
app.post('/devget',function(req,res,next){
var User = {
username: req.body.username,
password: req.body.password
};
app.service('dataget').find(User).then(user => {
db.view('byuser/user',{key: User.username}, function (err, docs) {
// Handle response
res.json(docs);
});
console.log('User Get Successfully.', user);
}).catch(console.error);
})
这会给我的数据响应为:
Response [
{ id: '060ab48a4826da7125d8ae45350037ee',
key: 'w',
value:
{ _id: '060ab48a4826da7125d8ae45350037ee',
_rev: '1-ea9a18d3724ce4542019dc5752c1fd4d',
username: 'w',
password: '$2a$10$yBJVJTmVXfTk0V4CCiWkd.GvAZZB9dF2pckKJ9wb/lJcAK8Ou.v06',
id: 0 } } ]
此工作正常,并通过字是加密的,但我没有得到如何解密密码和认证用户。
注意:我只想o用钩子和服务或定制服务或类来做,但不使用护照。
答
您不解密密码;您将加密的密码与将加密密码的功能(在您找到用户进行密码比较之后)进行比较。
var hash = bcrypt.hashSync("bacon");
bcrypt.compareSync("bacon", hash); // true
bcrypt.compareSync("veggies", hash); // false
您可以通过用户[feathers-authentication](https://github.com/feathersjs/feathers-authentication)来验证用户身份。如果你想创建你自己的,那么更好地使用你自己的密码哈希。但是,如果你会使用羽毛,然后检查他们如何哈希你的密码[这里](https://github.com/feathersjs/feathers-authentication-local/blob/master/src/utils/hash.js)。 – Jalil