我使用Feathers框架到我的nodejs应用程序,我已经按照以下方式创建了jwt
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解码数据从令牌获取userId。
答案 0 :(得分:0)
使用jwt-decode 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