2017-06-06 114 views
0

我是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用鉤子和服務或定製服務或類來做,但不使用護照。

+0

您可以通過用戶[feathers-authentication](https://github.com/feathersjs/feathers-authentication)來驗證用戶身份。如果你想創建你自己的,那麼更好地使用你自己的密碼哈希。但是,如果你會使用羽毛,然後檢查他們如何哈希你的密碼[這裏](https://github.com/feathersjs/feathers-authentication-local/blob/master/src/utils/hash.js)。 – Jalil

回答

0

您不解密密碼;您將加密的密碼與將加密密碼的功能(在您找到用戶進行密碼比較之後)進行比較。

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

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