簡體   English   中英

如何在feather js中解碼和驗證密碼

[英]How to decode and verify password in feathers js

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

這工作正常,密碼已加密,但我不知道如何解密密碼和驗證用戶。

注意:我只是想用鈎子和服務或定制服務或課程來做,但不使用護照。

您不解密密碼; 您將加密的密碼與加密密碼的函數進行比較(在您找到要進行密碼比較的用戶之后)。

const bcrypt = require('bcryptjs');


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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM