2017-09-25 58 views
0

我使用羽毛框架,以我的應用程序的NodeJS和我已經在下列方式如何解碼jwt並從feathersjs中獲取此userId?

app.service('authentication').hooks({ 
    before: { 
     create: [ 
     authentication.hooks.authenticate(config.strategies), 
     function (hook) { 
     hook.params.payload = { 
      userId: hook.params.user.userId, 
      accountId: hook.params.user.accountId 
      }; 
      return Promise.resolve(hook); 
     } 
     ], 
     remove: [ 
     authentication.hooks.authenticate('jwt') 
     ] 
    } 
    }); 

做智威湯遜的創作現在我有疑問,如何從令牌使用JWT獲得用戶id解碼後的數據。

回答

0

使用JWT解碼LIB將提供feathersjs的JWT解碼功能

const decode = require('jwt-decode'); 

hook.params.headers.authorization = 'eyJhbGciOiJIUzI1NiIsInR5cCI6ImFjY2VzcyIsInR5cGUiOiJhY2Nlc3MifQ.eyJ1c2VySWQiOjEsImFjY291bnRJZCI6MSwiaWF0IjoxNTA2MzMwNzIyLCJleHAiOjE1MDY0MTcxMjIsImF1ZCI6Imh0dHBzOi8veW91cmRvbWFpbi5jb20iLCJpc3MiOiJmZWF0aGVycyIsInN1YiI6ImFub255bW91cyJ9.gZrDJhmzdpt9-7OCeRcKiayQiKbtv-3UaTkN1BhOCAI' 

var userId = decode(hook.params.headers.authorization).userId; 
console.log(userId) 
1 
相關問題