【问题标题】:How to decode and verify password in feathers js如何在羽毛js中解码和验证密码
【发布时间】:2019-12-01 02:06:14
【问题描述】:

我是 feathersJs 的新手,正在尝试学习如何使用钩子和服务执行身份验证。我正在使用 Couchdb 数据库和底座。 这是使用“用户”挂钩服务在 hashPassword 中加密密码的 post 方法。 post方法如下:

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 } } ]

这工作正常,密码已加密,但我不知道如何解密密码和验证用户。

注意:我只想用钩子和服务或自定义服务或类来做,但不使用护照。

【问题讨论】:

  • 您可以使用feathers-authentication 对用户进行身份验证。如果您想创建自己的,那么最好使用您自己的密码哈希。但是,如果您将使用羽毛,请检查它们如何散列您的密码 here

标签: node.js authentication jwt feathersjs


【解决方案1】:

您没有解密密码;您将加密的密码与将加密密码的函数进行比较(在您找到要进行密码比较的用户之后)。

const bcrypt = require('bcryptjs');


var hash = bcrypt.hashSync("bacon");

bcrypt.compareSync("bacon", hash); // true
bcrypt.compareSync("veggies", hash); // false

【讨论】:

    猜你喜欢
    • 2022-06-27
    • 2019-08-07
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 2019-01-17
    • 2018-05-12
    相关资源
    最近更新 更多